{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7dde07b1",
   "metadata": {},
   "source": [
    "## Pandas简介"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "220c49ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nPandas是Python用于数据处理和数据分析的第三方库，它擅长于处理数字型数据、时间序列数据、文本型数据\\nPandas适合处理一个归正的一维和二维数据，类似于SQL执行后的产出，或者无合并单元格的excel表格这样的数据。\\n它可以将多个文件合并在一起，即使文件结构不一样，也能通过处理进行合并\\n\\n'"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "Pandas是Python用于数据处理和数据分析的第三方库，它擅长于处理数字型数据、时间序列数据、文本型数据\n",
    "Pandas适合处理一个归正的一维和二维数据，类似于SQL执行后的产出，或者无合并单元格的excel表格这样的数据。\n",
    "它可以将多个文件合并在一起，即使文件结构不一样，也能通过处理进行合并\n",
    "\n",
    "'''\n",
    "# Pandas是基于numpy数组构建的，但二者最大的不同是\n",
    "# pandas是专门为处理表格和混杂数据设计的，比较契合统计分析中的表结构，\n",
    "# 而numpy更适合处理统一的数值数组数据。\n",
    "# pandas数组结构有一维Series和二维DataFrame。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "d1126760",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Numpy有以下限制，Pandas再次基础上做了功能扩展\n",
    "\n",
    "# 不支持列名。\n",
    "# 所有元素的数据类型必须相同。\n",
    "# 没有用于常见分析任务的预构建方法。\n",
    "# Pandas可以轻松处理大量数据!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc6835b8",
   "metadata": {},
   "source": [
    "## Pandas的强大之处"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "1557755a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['年份', '国民总收入', '国内生产总值', '第一产业增加值', '第二产业增加值', '第三产业增加值', '人均国内生产总值'],\n",
       " ['2018', '896915.6', '900309.5', '64734', '366000.9', '469574.6', '64644'],\n",
       " ['2017', '820099.5', '820754.3', '62099.5', '332742.7', '425912.1', '59201'],\n",
       " ['2016', '737074', '740060.8', '60139.2', '296547.7', '383373.9', '53680'],\n",
       " ['2015', '683390.5', '685992.9', '57774.6', '282040.3', '346178', '50028'],\n",
       " ['2014', '642097.6', '641280.6', '55626.3', '277571.8', '308082.5', '47005'],\n",
       " ['2013', '588141.2', '592963.2', '53028.1', '261956.1', '277979.1', '43684'],\n",
       " ['2012', '537329', '538580', '49084.5', '244643.3', '244852.2', '39874'],\n",
       " ['2011', '483392.8', '487940.2', '44781.4', '227038.8', '216120', '36302'],\n",
       " ['2010', '410354.1', '412119.3', '38430.8', '191629.8', '182058.6', '30808'],\n",
       " ['2009', '347934.9', '348517.7', '33583.8', '160171.7', '154762.2', '26180'],\n",
       " ['2008', '321229.5', '319244.6', '32464.1', '149956.6', '136823.9', '24100'],\n",
       " ['2007', '270704', '270092.3', '27674.1', '126633.6', '115784.6', '20494'],\n",
       " ['2006', '219028.5', '219438.5', '23317', '104361.8', '91759.7', '16738'],\n",
       " ['2005', '185998.9', '187318.9', '21806.7', '88084.4', '77427.8', '14368'],\n",
       " ['2004', '161415.4', '161840.2', '20904.3', '74286.9', '66648.9', '12487'],\n",
       " ['2003', '136576.3', '137422', '16970.2', '62697.4', '57754.4', '10666'],\n",
       " ['2002', '120480.4', '121717.4', '16190.2', '54105.5', '51421.7', '9506'],\n",
       " ['2001', '109276.2', '110863.1', '15502.5', '49660.7', '45700', '8717'],\n",
       " ['2000', '99066.1', '100280.1', '14717.4', '45664.8', '39897.9', '7942'],\n",
       " ['1999', '89366.5', '90564.4', '14549', '41080.9', '34934.5', '7229'],\n",
       " ['1998', '83817.6', '85195.5', '14618.7', '39018.5', '31558.3', '6860'],\n",
       " ['1997', '78802.9', '79715', '14265.2', '37546', '27903.8', '6481'],\n",
       " ['1996', '70779.6', '71813.6', '13878.3', '33828.1', '24107.2', '5898'],\n",
       " ['1995', '60356.6', '61339.9', '12020.5', '28677.5', '20641.9', '5091'],\n",
       " ['1994', '48548.2', '48637.5', '9471.8', '22453.1', '16712.5', '4081'],\n",
       " ['1993', '35599.2', '35673.2', '6887.6', '16473.1', '12312.6', '3027'],\n",
       " ['1992', '27208.2', '27194.5', '5800.3', '11725.3', '9668.9', '2334'],\n",
       " ['1991', '22050.3', '22005.6', '5288.8', '9129.8', '7587', '1912'],\n",
       " ['1990', '18923.3', '18872.9', '5017.2', '7744.3', '6111.4', '1663'],\n",
       " ['1989', '17188.4', '17179.7', '4228.2', '7300.9', '5650.6', '1536'],\n",
       " ['1988', '15174.4', '15180.4', '3831.2', '6607.4', '4741.8', '1378'],\n",
       " ['1987', '12166.6', '12174.6', '3204.5', '5274', '3696.2', '1123'],\n",
       " ['1986', '10375.4', '10376.2', '2764.1', '4515.2', '3096.9', '973'],\n",
       " ['1985', '9123.6', '9098.9', '2541.7', '3886.5', '2670.7', '866'],\n",
       " ['1984', '7314.2', '7278.5', '2295.6', '3124.8', '1858.1', '702'],\n",
       " ['1983', '6043.8', '6020.9', '1960.9', '2663', '1397', '588'],\n",
       " ['1982', '5380.5', '5373.4', '1761.7', '2397.7', '1214', '533'],\n",
       " ['1981', '4933.7', '4935.8', '1545.7', '2269.1', '1121.1', '497'],\n",
       " ['1980', '4587.6', '4587.6', '1359.5', '2204.7', '1023.4', '468'],\n",
       " ['1979', '4100.5', '4100.5', '1259', '1925.4', '916.1', '423'],\n",
       " ['1978', '3678.7', '3678.7', '1018.5', '1755.2', '905.1', '385'],\n",
       " ['1977', '3250', '3250', '942.2', '1517.8', '790', '344'],\n",
       " ['1976', '2988.6', '2988.6', '967.1', '1346', '675.5', '321'],\n",
       " ['1975', '3039.5', '3039.5', '971.2', '1378.7', '689.6', '332'],\n",
       " ['1974', '2827.7', '2827.7', '945.2', '1199.8', '682.7', '314'],\n",
       " ['1973', '2756.2', '2756.2', '907.5', '1180.4', '668.3', '313'],\n",
       " ['1972', '2552.4', '2552.4', '827.4', '1091.6', '633.3', '296'],\n",
       " ['1971', '2456.9', '2456.9', '826.3', '1029.9', '600.6', '292'],\n",
       " ['1970', '2279.7', '2279.7', '793.3', '918.1', '568.3', '279'],\n",
       " ['1969', '1962.2', '1962.2', '736.2', '695', '531', '247'],\n",
       " ['1968', '1744.1', '1744.1', '726.3', '542.6', '475.2', '225'],\n",
       " ['1967', '1794.2', '1794.2', '714.2', '608', '472', '238'],\n",
       " ['1966', '1888.7', '1888.7', '702.2', '715.4', '471', '257'],\n",
       " ['1965', '1734', '1734', '651.1', '608.5', '474.4', '242'],\n",
       " ['1964', '1469.9', '1469.9', '559', '519.3', '391.6', '210'],\n",
       " ['1963', '1248.3', '1248.3', '497.5', '412.8', '337.9', '183'],\n",
       " ['1962', '1162.2', '1162.2', '453.1', '363.9', '345.1', '175'],\n",
       " ['1961', '1232.3', '1232.3', '441.1', '393.5', '397.7', '187'],\n",
       " ['1960', '1470.1', '1470.1', '340.7', '652.6', '476.8', '220'],\n",
       " ['1959', '1447.5', '1447.5', '383.8', '616.8', '446.9', '217'],\n",
       " ['1958', '1312.3', '1312.3', '445.9', '483.6', '382.8', '201'],\n",
       " ['1957', '1071.4', '1071.4', '430', '316.6', '324.8', '168'],\n",
       " ['1956', '1030.7', '1030.7', '443.9', '280.4', '306.4', '166'],\n",
       " ['1955', '911.6', '911.6', '421', '221.5', '269.1', '150'],\n",
       " ['1954', '859.8', '859.8', '392', '210.8', '257', '144'],\n",
       " ['1953', '824.4', '824.4', '378', '191.6', '254.8', '142'],\n",
       " ['1952', '679.1', '679.1', '342.9', '141.1', '195.1', '119']]"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接用Python基础语言读、写csv文件\n",
    "import csv\n",
    "\n",
    "def read_csv(file_name, mode='r', encoding='UTF-8', newline='', quotechar='\"', dialect='excel'):\n",
    "    \"\"\" 读取逗号分隔文本文件, 返回rows = [[row1], [row2], [row3], [row4], ...]\"\"\"\n",
    "    rows = []\n",
    "    if encoding is not None:\n",
    "        csv_file = open(file_name, mode=mode, newline=newline, encoding=encoding)\n",
    "        csvreader = csv.reader(csv_file, dialect=dialect, quotechar=quotechar)\n",
    "        rows = [row for row in csvreader]\n",
    "        csv_file.close()\n",
    "    else:\n",
    "        for encoding in ['UTF-8', 'UTF-8-sig', 'default', 'GB2312']:\n",
    "            if encoding == 'default':\n",
    "                csv_file = open(file_name, mode=mode, newline=newline)\n",
    "            else:\n",
    "                csv_file = open(file_name, mode=mode, newline=newline, encoding=encoding)\n",
    "            try:\n",
    "                csvreader = csv.reader(csv_file, dialect=dialect, quotechar=quotechar)\n",
    "                rows = [row for row in csvreader]\n",
    "                csv_file.close()\n",
    "                logger.info(f'csv file encoding: {encoding}')\n",
    "                break\n",
    "            except UnicodeDecodeError:\n",
    "                continue\n",
    "    return rows\n",
    "\n",
    "def write_csv(rows, file_name, mode='a', encoding='UTF-8'):\n",
    "    \"\"\"写入逗号分隔文本文件,输入rows为二维数组\"\"\"\n",
    "    with open(file_name, mode=mode, encoding=encoding, newline='') as csvfile:\n",
    "        csvwriter = csv.writer(csvfile, dialect='excel', quotechar='\"')\n",
    "        for row in rows:\n",
    "            csvwriter.writerow(row)\n",
    "            \n",
    "read_csv('GDP-China.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "fce9e82d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份</th>\n",
       "      <th>国民总收入</th>\n",
       "      <th>国内生产总值</th>\n",
       "      <th>第一产业增加值</th>\n",
       "      <th>第二产业增加值</th>\n",
       "      <th>第三产业增加值</th>\n",
       "      <th>人均国内生产总值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018</td>\n",
       "      <td>896915.6</td>\n",
       "      <td>900309.5</td>\n",
       "      <td>64734.0</td>\n",
       "      <td>366000.9</td>\n",
       "      <td>469574.6</td>\n",
       "      <td>64644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017</td>\n",
       "      <td>820099.5</td>\n",
       "      <td>820754.3</td>\n",
       "      <td>62099.5</td>\n",
       "      <td>332742.7</td>\n",
       "      <td>425912.1</td>\n",
       "      <td>59201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016</td>\n",
       "      <td>737074.0</td>\n",
       "      <td>740060.8</td>\n",
       "      <td>60139.2</td>\n",
       "      <td>296547.7</td>\n",
       "      <td>383373.9</td>\n",
       "      <td>53680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015</td>\n",
       "      <td>683390.5</td>\n",
       "      <td>685992.9</td>\n",
       "      <td>57774.6</td>\n",
       "      <td>282040.3</td>\n",
       "      <td>346178.0</td>\n",
       "      <td>50028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014</td>\n",
       "      <td>642097.6</td>\n",
       "      <td>641280.6</td>\n",
       "      <td>55626.3</td>\n",
       "      <td>277571.8</td>\n",
       "      <td>308082.5</td>\n",
       "      <td>47005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>443.9</td>\n",
       "      <td>280.4</td>\n",
       "      <td>306.4</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955</td>\n",
       "      <td>911.6</td>\n",
       "      <td>911.6</td>\n",
       "      <td>421.0</td>\n",
       "      <td>221.5</td>\n",
       "      <td>269.1</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954</td>\n",
       "      <td>859.8</td>\n",
       "      <td>859.8</td>\n",
       "      <td>392.0</td>\n",
       "      <td>210.8</td>\n",
       "      <td>257.0</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953</td>\n",
       "      <td>824.4</td>\n",
       "      <td>824.4</td>\n",
       "      <td>378.0</td>\n",
       "      <td>191.6</td>\n",
       "      <td>254.8</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952</td>\n",
       "      <td>679.1</td>\n",
       "      <td>679.1</td>\n",
       "      <td>342.9</td>\n",
       "      <td>141.1</td>\n",
       "      <td>195.1</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      年份     国民总收入    国内生产总值  第一产业增加值   第二产业增加值   第三产业增加值  人均国内生产总值\n",
       "0   2018  896915.6  900309.5  64734.0  366000.9  469574.6     64644\n",
       "1   2017  820099.5  820754.3  62099.5  332742.7  425912.1     59201\n",
       "2   2016  737074.0  740060.8  60139.2  296547.7  383373.9     53680\n",
       "3   2015  683390.5  685992.9  57774.6  282040.3  346178.0     50028\n",
       "4   2014  642097.6  641280.6  55626.3  277571.8  308082.5     47005\n",
       "..   ...       ...       ...      ...       ...       ...       ...\n",
       "62  1956    1030.7    1030.7    443.9     280.4     306.4       166\n",
       "63  1955     911.6     911.6    421.0     221.5     269.1       150\n",
       "64  1954     859.8     859.8    392.0     210.8     257.0       144\n",
       "65  1953     824.4     824.4    378.0     191.6     254.8       142\n",
       "66  1952     679.1     679.1    342.9     141.1     195.1       119\n",
       "\n",
       "[67 rows x 7 columns]"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas方法\n",
    "import pandas as pd\n",
    "pd.read_csv('GDP-China.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6109cd2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "3ec8c66f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:18:07.171627Z",
     "start_time": "2022-05-07T08:18:06.468197Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "352d8435",
   "metadata": {},
   "source": [
    "## Pandas文件数据的读取与导出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8524e2d4",
   "metadata": {},
   "source": [
    "### 创建s/df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "b232060b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T09:48:02.931018Z",
     "start_time": "2022-05-09T09:48:02.882664Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    a\n",
       "2    b\n",
       "dtype: object"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# series\n",
    "import pandas as pd\n",
    "s1 = pd.Series(['a','b'])\n",
    "s2 = pd.Series(['c','d'])\n",
    "\n",
    "# 数组array->Series\n",
    "array = ['a','b']\n",
    "s1 = pd.Series(array, index = [1,2])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "36400ffe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T09:48:16.092816Z",
     "start_time": "2022-05-09T09:48:16.034888Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "x  1  4\n",
       "y  2  5\n",
       "z  3  6"
      ]
     },
     "execution_count": 163,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dataframe\n",
    "df = pd.DataFrame({'A': [1, 2, 3],    # A列\n",
    "                   'B': [4, 5, 6]},   # B列\n",
    "                  index=['x', 'y', 'z']  # 行索引\n",
    "                 )\n",
    "\n",
    "# 数组array->DataFrame\n",
    "df = pd.DataFrame(np.arange(12).reshape(3,4),\n",
    "                 columns=['A','B','C','D'])\n",
    "array = np.arange(12).reshape(3,4)\n",
    "df = pd.DataFrame(array, columns=['A','B','C','D'])\n",
    "df\n",
    "\n",
    "# 字典dict->DataFrame\n",
    "# pd.DataFrame(dict, index=['x', 'y', 'z'])\n",
    "my_dirt = {'A': [1, 2, 3],\n",
    "          'B': [4, 5, 6]}\n",
    "df = pd.DataFrame(my_dirt, \n",
    "                 index = ['x', 'y', 'z'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "952fe76e",
   "metadata": {},
   "source": [
    "### csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "448039e3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:40:43.541246Z",
     "start_time": "2022-05-07T06:40:43.481687Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份</th>\n",
       "      <th>国民总收入</th>\n",
       "      <th>国内生产总值</th>\n",
       "      <th>第一产业增加值</th>\n",
       "      <th>第二产业增加值</th>\n",
       "      <th>第三产业增加值</th>\n",
       "      <th>人均国内生产总值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018</td>\n",
       "      <td>896915.6</td>\n",
       "      <td>900309.5</td>\n",
       "      <td>64734.0</td>\n",
       "      <td>366000.9</td>\n",
       "      <td>469574.6</td>\n",
       "      <td>64644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017</td>\n",
       "      <td>820099.5</td>\n",
       "      <td>820754.3</td>\n",
       "      <td>62099.5</td>\n",
       "      <td>332742.7</td>\n",
       "      <td>425912.1</td>\n",
       "      <td>59201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016</td>\n",
       "      <td>737074.0</td>\n",
       "      <td>740060.8</td>\n",
       "      <td>60139.2</td>\n",
       "      <td>296547.7</td>\n",
       "      <td>383373.9</td>\n",
       "      <td>53680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015</td>\n",
       "      <td>683390.5</td>\n",
       "      <td>685992.9</td>\n",
       "      <td>57774.6</td>\n",
       "      <td>282040.3</td>\n",
       "      <td>346178.0</td>\n",
       "      <td>50028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014</td>\n",
       "      <td>642097.6</td>\n",
       "      <td>641280.6</td>\n",
       "      <td>55626.3</td>\n",
       "      <td>277571.8</td>\n",
       "      <td>308082.5</td>\n",
       "      <td>47005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>443.9</td>\n",
       "      <td>280.4</td>\n",
       "      <td>306.4</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955</td>\n",
       "      <td>911.6</td>\n",
       "      <td>911.6</td>\n",
       "      <td>421.0</td>\n",
       "      <td>221.5</td>\n",
       "      <td>269.1</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954</td>\n",
       "      <td>859.8</td>\n",
       "      <td>859.8</td>\n",
       "      <td>392.0</td>\n",
       "      <td>210.8</td>\n",
       "      <td>257.0</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953</td>\n",
       "      <td>824.4</td>\n",
       "      <td>824.4</td>\n",
       "      <td>378.0</td>\n",
       "      <td>191.6</td>\n",
       "      <td>254.8</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952</td>\n",
       "      <td>679.1</td>\n",
       "      <td>679.1</td>\n",
       "      <td>342.9</td>\n",
       "      <td>141.1</td>\n",
       "      <td>195.1</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      年份     国民总收入    国内生产总值  第一产业增加值   第二产业增加值   第三产业增加值  人均国内生产总值\n",
       "0   2018  896915.6  900309.5  64734.0  366000.9  469574.6     64644\n",
       "1   2017  820099.5  820754.3  62099.5  332742.7  425912.1     59201\n",
       "2   2016  737074.0  740060.8  60139.2  296547.7  383373.9     53680\n",
       "3   2015  683390.5  685992.9  57774.6  282040.3  346178.0     50028\n",
       "4   2014  642097.6  641280.6  55626.3  277571.8  308082.5     47005\n",
       "..   ...       ...       ...      ...       ...       ...       ...\n",
       "62  1956    1030.7    1030.7    443.9     280.4     306.4       166\n",
       "63  1955     911.6     911.6    421.0     221.5     269.1       150\n",
       "64  1954     859.8     859.8    392.0     210.8     257.0       144\n",
       "65  1953     824.4     824.4    378.0     191.6     254.8       142\n",
       "66  1952     679.1     679.1    342.9     141.1     195.1       119\n",
       "\n",
       "[67 rows x 7 columns]"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = 'GDP-China.csv'\n",
    "pd.read_csv(data)\n",
    "# 或者\n",
    "pd.read_csv('GDP-China.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "9e2d8112",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T01:47:32.545574Z",
     "start_time": "2022-05-06T01:47:32.479529Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\Ipython\\PythonLibDoc\\Pandas\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份</th>\n",
       "      <th>国民总收入</th>\n",
       "      <th>国内生产总值</th>\n",
       "      <th>第一产业增加值</th>\n",
       "      <th>第二产业增加值</th>\n",
       "      <th>第三产业增加值</th>\n",
       "      <th>人均国内生产总值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018</td>\n",
       "      <td>896915.6</td>\n",
       "      <td>900309.5</td>\n",
       "      <td>64734.0</td>\n",
       "      <td>366000.9</td>\n",
       "      <td>469574.6</td>\n",
       "      <td>64644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017</td>\n",
       "      <td>820099.5</td>\n",
       "      <td>820754.3</td>\n",
       "      <td>62099.5</td>\n",
       "      <td>332742.7</td>\n",
       "      <td>425912.1</td>\n",
       "      <td>59201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016</td>\n",
       "      <td>737074.0</td>\n",
       "      <td>740060.8</td>\n",
       "      <td>60139.2</td>\n",
       "      <td>296547.7</td>\n",
       "      <td>383373.9</td>\n",
       "      <td>53680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015</td>\n",
       "      <td>683390.5</td>\n",
       "      <td>685992.9</td>\n",
       "      <td>57774.6</td>\n",
       "      <td>282040.3</td>\n",
       "      <td>346178.0</td>\n",
       "      <td>50028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014</td>\n",
       "      <td>642097.6</td>\n",
       "      <td>641280.6</td>\n",
       "      <td>55626.3</td>\n",
       "      <td>277571.8</td>\n",
       "      <td>308082.5</td>\n",
       "      <td>47005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>443.9</td>\n",
       "      <td>280.4</td>\n",
       "      <td>306.4</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955</td>\n",
       "      <td>911.6</td>\n",
       "      <td>911.6</td>\n",
       "      <td>421.0</td>\n",
       "      <td>221.5</td>\n",
       "      <td>269.1</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954</td>\n",
       "      <td>859.8</td>\n",
       "      <td>859.8</td>\n",
       "      <td>392.0</td>\n",
       "      <td>210.8</td>\n",
       "      <td>257.0</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953</td>\n",
       "      <td>824.4</td>\n",
       "      <td>824.4</td>\n",
       "      <td>378.0</td>\n",
       "      <td>191.6</td>\n",
       "      <td>254.8</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952</td>\n",
       "      <td>679.1</td>\n",
       "      <td>679.1</td>\n",
       "      <td>342.9</td>\n",
       "      <td>141.1</td>\n",
       "      <td>195.1</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      年份     国民总收入    国内生产总值  第一产业增加值   第二产业增加值   第三产业增加值  人均国内生产总值\n",
       "0   2018  896915.6  900309.5  64734.0  366000.9  469574.6     64644\n",
       "1   2017  820099.5  820754.3  62099.5  332742.7  425912.1     59201\n",
       "2   2016  737074.0  740060.8  60139.2  296547.7  383373.9     53680\n",
       "3   2015  683390.5  685992.9  57774.6  282040.3  346178.0     50028\n",
       "4   2014  642097.6  641280.6  55626.3  277571.8  308082.5     47005\n",
       "..   ...       ...       ...      ...       ...       ...       ...\n",
       "62  1956    1030.7    1030.7    443.9     280.4     306.4       166\n",
       "63  1955     911.6     911.6    421.0     221.5     269.1       150\n",
       "64  1954     859.8     859.8    392.0     210.8     257.0       144\n",
       "65  1953     824.4     824.4    378.0     191.6     254.8       142\n",
       "66  1952     679.1     679.1    342.9     141.1     195.1       119\n",
       "\n",
       "[67 rows x 7 columns]"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看当前文档所在目录\n",
    "print(os.path.abspath('.')) \n",
    "# 使用绝对路径进行文件的读取\n",
    "pd.read_csv('D:\\Ipython\\PythonLibDoc\\Pandas\\GDP-China.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "0df3f770",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T01:47:33.348840Z",
     "start_time": "2022-05-06T01:47:33.312538Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份,国民总收入,国内生产总值,第一产业增加值,第二产业增加值,第三产业增加值,人均国内生产总值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018,896915.6,900309.5,64734,366000.9,469574.6...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017,820099.5,820754.3,62099.5,332742.7,425912...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016,737074,740060.8,60139.2,296547.7,383373.9...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015,683390.5,685992.9,57774.6,282040.3,346178...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014,642097.6,641280.6,55626.3,277571.8,308082...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956,1030.7,1030.7,443.9,280.4,306.4,166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955,911.6,911.6,421,221.5,269.1,150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954,859.8,859.8,392,210.8,257,144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953,824.4,824.4,378,191.6,254.8,142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952,679.1,679.1,342.9,141.1,195.1,119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     年份,国民总收入,国内生产总值,第一产业增加值,第二产业增加值,第三产业增加值,人均国内生产总值\n",
       "0   2018,896915.6,900309.5,64734,366000.9,469574.6...\n",
       "1   2017,820099.5,820754.3,62099.5,332742.7,425912...\n",
       "2   2016,737074,740060.8,60139.2,296547.7,383373.9...\n",
       "3   2015,683390.5,685992.9,57774.6,282040.3,346178...\n",
       "4   2014,642097.6,641280.6,55626.3,277571.8,308082...\n",
       "..                                                ...\n",
       "62           1956,1030.7,1030.7,443.9,280.4,306.4,166\n",
       "63               1955,911.6,911.6,421,221.5,269.1,150\n",
       "64                 1954,859.8,859.8,392,210.8,257,144\n",
       "65               1953,824.4,824.4,378,191.6,254.8,142\n",
       "66             1952,679.1,679.1,342.9,141.1,195.1,119\n",
       "\n",
       "[67 rows x 1 columns]"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定分隔符号\n",
    "data = 'GDP-China.csv'\n",
    "pd.read_csv(data, sep= '\\t') # 使用制表符进行分隔\n",
    "# 或者使用read_table，默认制表符是tab\n",
    "pd.read_table(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "83664e32",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = 'GDP-China.csv'\n",
    "df = pd.read_csv(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "3631f94c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T01:47:33.921877Z",
     "start_time": "2022-05-06T01:47:33.861749Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份</th>\n",
       "      <th>国民总收入</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018</td>\n",
       "      <td>896915.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017</td>\n",
       "      <td>820099.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016</td>\n",
       "      <td>737074.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015</td>\n",
       "      <td>683390.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014</td>\n",
       "      <td>642097.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956</td>\n",
       "      <td>1030.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955</td>\n",
       "      <td>911.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954</td>\n",
       "      <td>859.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953</td>\n",
       "      <td>824.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952</td>\n",
       "      <td>679.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      年份     国民总收入\n",
       "0   2018  896915.6\n",
       "1   2017  820099.5\n",
       "2   2016  737074.0\n",
       "3   2015  683390.5\n",
       "4   2014  642097.6\n",
       "..   ...       ...\n",
       "62  1956    1030.7\n",
       "63  1955     911.6\n",
       "64  1954     859.8\n",
       "65  1953     824.4\n",
       "66  1952     679.1\n",
       "\n",
       "[67 rows x 2 columns]"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定列，指定索引，指定名称， 调整列的顺序\n",
    "\n",
    "# pd.read_csv(data, header=None) # 默认不带Header参数，如将header参数设置为None，表示删除表头\n",
    "\n",
    "# pd.read_csv(data, names=['年份', '国民总收入']) # 指定列名，列表\n",
    "\n",
    "pd.read_csv(data, usecols=['年份', '国民总收入']) # 只读取指定列，其实就是筛选功能\n",
    "\n",
    "# pd.read_csv(data, usecols=['年份', '国民总收入'])[ ['国民总收入', '年份'] ]#  调整列的顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "09f6159b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T01:47:55.440318Z",
     "start_time": "2022-05-06T01:47:55.383386Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年份</th>\n",
       "      <th>国民总收入</th>\n",
       "      <th>国内生产总值</th>\n",
       "      <th>第一产业增加值</th>\n",
       "      <th>第二产业增加值</th>\n",
       "      <th>第三产业增加值</th>\n",
       "      <th>人均国内生产总值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018.0</td>\n",
       "      <td>896915.6</td>\n",
       "      <td>900309.5</td>\n",
       "      <td>64734.0</td>\n",
       "      <td>366000.9</td>\n",
       "      <td>469574.6</td>\n",
       "      <td>64644.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017.0</td>\n",
       "      <td>820099.5</td>\n",
       "      <td>820754.3</td>\n",
       "      <td>62099.5</td>\n",
       "      <td>332742.7</td>\n",
       "      <td>425912.1</td>\n",
       "      <td>59201.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016.0</td>\n",
       "      <td>737074.0</td>\n",
       "      <td>740060.8</td>\n",
       "      <td>60139.2</td>\n",
       "      <td>296547.7</td>\n",
       "      <td>383373.9</td>\n",
       "      <td>53680.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015.0</td>\n",
       "      <td>683390.5</td>\n",
       "      <td>685992.9</td>\n",
       "      <td>57774.6</td>\n",
       "      <td>282040.3</td>\n",
       "      <td>346178.0</td>\n",
       "      <td>50028.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014.0</td>\n",
       "      <td>642097.6</td>\n",
       "      <td>641280.6</td>\n",
       "      <td>55626.3</td>\n",
       "      <td>277571.8</td>\n",
       "      <td>308082.5</td>\n",
       "      <td>47005.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>1956.0</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>1030.7</td>\n",
       "      <td>443.9</td>\n",
       "      <td>280.4</td>\n",
       "      <td>306.4</td>\n",
       "      <td>166.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>1955.0</td>\n",
       "      <td>911.6</td>\n",
       "      <td>911.6</td>\n",
       "      <td>421.0</td>\n",
       "      <td>221.5</td>\n",
       "      <td>269.1</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>1954.0</td>\n",
       "      <td>859.8</td>\n",
       "      <td>859.8</td>\n",
       "      <td>392.0</td>\n",
       "      <td>210.8</td>\n",
       "      <td>257.0</td>\n",
       "      <td>144.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>1953.0</td>\n",
       "      <td>824.4</td>\n",
       "      <td>824.4</td>\n",
       "      <td>378.0</td>\n",
       "      <td>191.6</td>\n",
       "      <td>254.8</td>\n",
       "      <td>142.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>1952.0</td>\n",
       "      <td>679.1</td>\n",
       "      <td>679.1</td>\n",
       "      <td>342.9</td>\n",
       "      <td>141.1</td>\n",
       "      <td>195.1</td>\n",
       "      <td>119.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        年份     国民总收入    国内生产总值  第一产业增加值   第二产业增加值   第三产业增加值  人均国内生产总值\n",
       "0   2018.0  896915.6  900309.5  64734.0  366000.9  469574.6   64644.0\n",
       "1   2017.0  820099.5  820754.3  62099.5  332742.7  425912.1   59201.0\n",
       "2   2016.0  737074.0  740060.8  60139.2  296547.7  383373.9   53680.0\n",
       "3   2015.0  683390.5  685992.9  57774.6  282040.3  346178.0   50028.0\n",
       "4   2014.0  642097.6  641280.6  55626.3  277571.8  308082.5   47005.0\n",
       "..     ...       ...       ...      ...       ...       ...       ...\n",
       "62  1956.0    1030.7    1030.7    443.9     280.4     306.4     166.0\n",
       "63  1955.0     911.6     911.6    421.0     221.5     269.1     150.0\n",
       "64  1954.0     859.8     859.8    392.0     210.8     257.0     144.0\n",
       "65  1953.0     824.4     824.4    378.0     191.6     254.8     142.0\n",
       "66  1952.0     679.1     679.1    342.9     141.1     195.1     119.0\n",
       "\n",
       "[67 rows x 7 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定数据类型\n",
    "pd.read_csv(data, dtype=np.float64) # 所有数据均改为float64数据类型\n",
    "# pd.read_csv(data, dtype={'国民总收入':np.float64, '国内生产总值':np.float64}) # 指定列，设置数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4ebbe396",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T01:54:40.078177Z",
     "start_time": "2022-05-06T01:54:40.060566Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导出文件\n",
    "# df.to_csv('new.csv') \n",
    "df.to_csv('new.csv, index = False')  # 删除索引，第一列不会出现 0，1，2，3等等索引列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f3ebf26",
   "metadata": {},
   "source": [
    "### xlsx文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1c4cb831",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:22:23.373802Z",
     "start_time": "2022-05-09T02:22:23.275888Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Sheet1':         name team  Q1  Q2  Q3  Q4\n",
       " 0      Liver    E  89  21  24  64\n",
       " 1       Arry    C  36  37  37  57\n",
       " 2        Ack    A  57  60  18  84\n",
       " 3      Eorge    C  93  96  71  78\n",
       " 4        Oah    D  65  49  61  86\n",
       " ..       ...  ...  ..  ..  ..  ..\n",
       " 95   Gabriel    C  48  59  87  74\n",
       " 96   Austin7    C  21  31  30  43\n",
       " 97  Lincoln4    C  98  93   1  20\n",
       " 98       Eli    E  11  74  58  91\n",
       " 99       Ben    E  21  43  41  74\n",
       " \n",
       " [100 rows x 6 columns],\n",
       " 'Sheet2':     name team  Q1  Q2  Q3  Q4\n",
       " 0  Liver    E  89  21  24  64}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取文件\n",
    "pd.read_excel('team.xlsx', sheet_name= 'Sheet1') # 指定sheet\n",
    "pd.read_excel(r'D:\\Ipython\\PythonLibDoc\\Pandas\\team.xlsx', sheet_name= 'Sheet1')# 如果读取window系统的文件,需要加r\n",
    "pd.read_excel('team.xlsx', sheet_name = ['Sheet1', 'Sheet2']) # 读取多个sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f69aa3dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导出xlsx\n",
    "df.to_excel('new.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c5ba31",
   "metadata": {},
   "source": [
    "### 数据库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c2c5ae97",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T02:32:43.353875Z",
     "start_time": "2022-05-06T02:32:43.346560Z"
    }
   },
   "outputs": [],
   "source": [
    "# 详见： python处理mysql，Python处理大数据hive数据仓库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a01940c",
   "metadata": {},
   "source": [
    "### 总结"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d5374a79",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T09:37:45.587286Z",
     "start_time": "2022-05-09T09:37:45.564820Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "Pandas一般用于数据清洗， \n",
    "Pandas的输出结果常作为Pyechart，sklearn等库的数据源，\n",
    "\n",
    "但是， Pandas的输出结果为Dataframe或者Series，\n",
    "Pyechart，sklearn等库所需的数据类型为list，\n",
    "\n",
    "通过DataFrame-Series-list进行一系列的转换\n",
    "'''\n",
    "\n",
    "# 下面我们来看下\n",
    "# 读取数据\n",
    "data = 'GDP-China.csv'\n",
    "df = pd.read_csv(data)\n",
    "# df \n",
    "type(df)  # pandas.core.series.Series\n",
    "\n",
    "# 获取数据中的有用列\n",
    "df['年份']\n",
    "type(df['年份']) # pandas.core.series.Series\n",
    "\n",
    "# 将array转换成list\n",
    "year = df['年份'].tolist()\n",
    "type(year)\n",
    "\n",
    "\n",
    "# array数组也是一种， 一些情况下，可以将数据转换成array\n",
    "year = np.array(df['年份'])\n",
    "type(year)  # numpy.ndarray  数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1658fd56",
   "metadata": {},
   "source": [
    "##  Pandas和Numpy之间的互相转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7c407189",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'pandas' has no attribute 'values'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[1;32mIn [32]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 1. 将Pandas类型转换为numpy类型，通过.values来转换：\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m np \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 2. 将numpy类型转换为list类型,通过.tolist()方法转换：\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mlist\u001b[39m \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mtolist()\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\__init__.py:261\u001b[0m, in \u001b[0;36m__getattr__\u001b[1;34m(name)\u001b[0m\n\u001b[0;32m    257\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01marrays\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msparse\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SparseArray \u001b[38;5;28;01mas\u001b[39;00m _SparseArray\n\u001b[0;32m    259\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m _SparseArray\n\u001b[1;32m--> 261\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodule \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpandas\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m has no attribute \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'pandas' has no attribute 'values'"
     ]
    }
   ],
   "source": [
    "# 1. 将Pandas类型转换为numpy类型，通过.values来转换：\n",
    "np = pd.values\n",
    "\n",
    "# 2. 将numpy类型转换为list类型,通过.tolist()方法转换：\n",
    "list = np.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb148a1c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T02:42:09.486994Z",
     "start_time": "2022-05-06T02:42:09.472728Z"
    }
   },
   "source": [
    "## Pandas数据信息查看"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d173e216",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T06:46:45.938140Z",
     "start_time": "2022-05-06T06:46:45.869355Z"
    }
   },
   "source": [
    "### 查看数据的列头"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "77359777",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89  21  24  64\n",
       "1       Arry    C  36  37  37  57\n",
       "2        Ack    A  57  60  18  84\n",
       "3      Eorge    C  93  96  71  78\n",
       "4        Oah    D  65  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C  48  59  87  74\n",
       "96   Austin7    C  21  31  30  43\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "98       Eli    E  11  74  58  91\n",
       "99       Ben    E  21  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "3fec7c2f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:14:43.871048Z",
     "start_time": "2022-05-06T07:14:43.799711Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['name', 'team', 'Q1', 'Q2', 'Q3', 'Q4'], dtype='object')"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df.columns  # 查看列头"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c807186",
   "metadata": {},
   "source": [
    "### 查看数据样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "03cccdaf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T06:50:36.882345Z",
     "start_time": "2022-05-06T06:50:36.852637Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>Albie1</td>\n",
       "      <td>D</td>\n",
       "      <td>79</td>\n",
       "      <td>82</td>\n",
       "      <td>56</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>Alex</td>\n",
       "      <td>D</td>\n",
       "      <td>14</td>\n",
       "      <td>70</td>\n",
       "      <td>55</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      name team  Q1  Q2  Q3  Q4\n",
       "57  Albie1    D  79  82  56  96\n",
       "63    Alex    D  14  70  55  87\n",
       "2      Ack    A  57  60  18  84"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "\n",
    "# 取部分数据（头、尾、随机）\n",
    "# df.head() # 查看前五条数据\n",
    "# df.head(10) # 查看前10条数据\n",
    "# df.tail() # 查看后五条数据\n",
    "# df.tail(10) # 查看后十条数据\n",
    "# df.sample() # 随机查看一条数据\n",
    "df.sample(3) # 随机查看三条数据\n",
    "# df.sample(10, ignore_index=True) # 随机查看数据并重置索引\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6625797",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T06:52:06.174385Z",
     "start_time": "2022-05-06T06:52:06.097224Z"
    }
   },
   "outputs": [],
   "source": [
    "# 取指定列数据\n",
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "s = df.Q1\n",
    "s = df['Q1']\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b786785",
   "metadata": {},
   "source": [
    "### 查看数据形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "490c0d3f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:16:56.308653Z",
     "start_time": "2022-05-06T07:16:56.259506Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 6)"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数据形状\n",
    "df.shape # dataframe数据形状是（x,y）\n",
    "# s.shape # series数据形状是（x,）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "036bbea6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:17:38.654020Z",
     "start_time": "2022-05-06T07:17:38.639310Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数据维数\n",
    "df.ndim\n",
    "# s.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed76c2f5",
   "metadata": {},
   "source": [
    "### 查看基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "b3516dec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:37:31.694045Z",
     "start_time": "2022-05-09T06:37:31.663584Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 100 entries, 0 to 99\n",
      "Data columns (total 6 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   name    100 non-null    object\n",
      " 1   team    100 non-null    object\n",
      " 2   Q1      100 non-null    int64 \n",
      " 3   Q2      100 non-null    int64 \n",
      " 4   Q3      100 non-null    int64 \n",
      " 5   Q4      100 non-null    int64 \n",
      "dtypes: int64(4), object(2)\n",
      "memory usage: 4.8+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58155e8f",
   "metadata": {},
   "source": [
    "### 数据类型 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "id": "bb6fc986",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:09:36.136845Z",
     "start_time": "2022-05-06T07:09:36.101380Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name    object\n",
       "team    object\n",
       "Q1       int64\n",
       "Q2       int64\n",
       "Q3       int64\n",
       "Q4       int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes  # dataframe使用dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a895893b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:10:32.460453Z",
     "start_time": "2022-05-06T07:10:32.451744Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dtype # Series使用dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1726a9b9",
   "metadata": {},
   "source": [
    "### 行列索引内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "8b21fc83",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:12:14.566183Z",
     "start_time": "2022-05-06T07:12:14.552903Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[RangeIndex(start=0, stop=100, step=1),\n",
       " Index(['name', 'team', 'Q1', 'Q2', 'Q3', 'Q4'], dtype='object')]"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.axes   # 列索引+ 行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bb450fa2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:12:27.560447Z",
     "start_time": "2022-05-06T07:12:27.551312Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[RangeIndex(start=0, stop=100, step=1)]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.axes # 列索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1072cdd9",
   "metadata": {},
   "source": [
    "### 数据转成列表矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "f7c14e14",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:16:23.359818Z",
     "start_time": "2022-05-06T07:16:23.344876Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "df.to_numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28a8ae28-f8cf-4244-8ba6-d08be760333f",
   "metadata": {},
   "source": [
    "### pandas如何显示全部数据而不是省略号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27daa508-54b5-4a01-9baf-a671851f3ff8",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# pandas 数据\n",
    "# 默认情况下，pandas会自动将DataFrame中的某些行和列缩略，并以省略号“...”表示。为了显示全部数据而不是省略号，可以使用以下代码：\n",
    "\n",
    "\n",
    "# 设置pandas显示所有列\n",
    "pd.set_option('display.max_columns', None)\n",
    "# 设置pandas显示所有行\n",
    "pd.set_option('display.max_rows', None)\n",
    "# 设置pandas显示所有字符\n",
    "pd.set_option('display.max_colwidth', -1)\n",
    "将以上代码放在pandas库导入语句后，DataFrame输出时就会显示全部数据而不是省略号。注意，这个方法虽然可以显示全部数据，但是可能会占用更多的内存和计算资源。下面是示例代码：\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "# 读取数据\n",
    "data = pd.read_csv('data.csv')\n",
    "# 设置pandas显示所有列\n",
    "pd.set_option('display.max_columns', None)\n",
    "# 设置pandas显示所有行\n",
    "pd.set_option('display.max_rows', None)\n",
    "# 设置pandas显示所有字符\n",
    "pd.set_option('display.max_colwidth', -1)\n",
    "# 显示全部数据\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ecd243b-e645-4f4a-94fa-4cc6c27ab523",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e0d56409",
   "metadata": {},
   "source": [
    "## Pandas数学统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "bacd0a9d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:22:49.927004Z",
     "start_time": "2022-05-06T07:22:49.920749Z"
    }
   },
   "outputs": [],
   "source": [
    "# Pandas可以对Series和Dataframe进行快速的描述性统计，比如求和、平均数、最大值、方差等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6db55ed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:23:28.082689Z",
     "start_time": "2022-05-06T07:23:28.074169Z"
    }
   },
   "source": [
    "### 数据概况 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "5d0c271c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:24:11.810437Z",
     "start_time": "2022-05-06T07:24:11.759004Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>49.200000</td>\n",
       "      <td>52.550000</td>\n",
       "      <td>52.670000</td>\n",
       "      <td>52.780000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>29.962603</td>\n",
       "      <td>29.845181</td>\n",
       "      <td>26.543677</td>\n",
       "      <td>27.818524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>19.500000</td>\n",
       "      <td>26.750000</td>\n",
       "      <td>29.500000</td>\n",
       "      <td>29.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>51.500000</td>\n",
       "      <td>49.500000</td>\n",
       "      <td>55.000000</td>\n",
       "      <td>53.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>74.250000</td>\n",
       "      <td>77.750000</td>\n",
       "      <td>76.250000</td>\n",
       "      <td>75.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>98.000000</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>99.000000</td>\n",
       "      <td>99.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Q1          Q2          Q3          Q4\n",
       "count  100.000000  100.000000  100.000000  100.000000\n",
       "mean    49.200000   52.550000   52.670000   52.780000\n",
       "std     29.962603   29.845181   26.543677   27.818524\n",
       "min      1.000000    1.000000    1.000000    2.000000\n",
       "25%     19.500000   26.750000   29.500000   29.500000\n",
       "50%     51.500000   49.500000   55.000000   53.000000\n",
       "75%     74.250000   77.750000   76.250000   75.250000\n",
       "max     98.000000   99.000000   99.000000   99.000000"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()\n",
    "# s.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c129bbd",
   "metadata": {},
   "source": [
    "### 数学统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "id": "6db265e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:27:58.159781Z",
     "start_time": "2022-05-06T07:27:58.130039Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\10191977\\AppData\\Local\\Temp\\ipykernel_11664\\3068083035.py:2: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.\n",
      "  df.mean(1).head(100)  # 每行的平均数\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0     49.50\n",
       "1     41.75\n",
       "2     54.75\n",
       "3     84.50\n",
       "4     65.25\n",
       "      ...  \n",
       "95    67.00\n",
       "96    31.25\n",
       "97    53.00\n",
       "98    58.50\n",
       "99    44.75\n",
       "Length: 100, dtype: float64"
      ]
     },
     "execution_count": 194,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.mean()  # 每列的平均数\n",
    "df.mean(1).head()  # 每行的平均数,head参数默认为5\n",
    "# s.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "772ba3a2",
   "metadata": {},
   "source": [
    "### 统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "31298ede",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:29:28.105058Z",
     "start_time": "2022-05-06T07:29:28.069151Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\10191977\\AppData\\Local\\Temp\\ipykernel_11664\\1813456900.py:11: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.\n",
      "  df.std() # 返回每一列的标准差, 贝塞尔校正的样本标准偏差\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Q1    29.962603\n",
       "Q2    29.845181\n",
       "Q3    26.543677\n",
       "Q4    27.818524\n",
       "dtype: float64"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# Pandas 内置很多数学计算方法：\n",
    "\n",
    "# df.mean() # 返回所有列的均值\n",
    "# df.mean(1) # 返回所有行的均值，下同\n",
    "# df.corr() # 返回列与列之间的相关系数\n",
    "# df.count() # 返回每一列中的非空值的个数\n",
    "# df.max() # 返回每一列的最大值\n",
    "# df.min() # 返回每一列的最小值\n",
    "# df.abs() # 绝对值\n",
    "# df.median() # 返回每一列的中位数\n",
    "# df.std() # 返回每一列的标准差, 贝塞尔校正的样本标准偏差\n",
    "# df.var() # 无偏方差\n",
    "# df.sem() # 平均值的标准误差\n",
    "# df.mode() # 众数\n",
    "# df.prod() # 连乘\n",
    "# df.mad() # 平均绝对偏差\n",
    "# df.cumprod() # 累积连乘,累乘\n",
    "# df.cumsum(axis=0) # 累积连加,累加\n",
    "# df.nunique() # 去重数量，不同值的数量\n",
    "# df.drop_duplicates() # 去重数量，不同值的清单\n",
    "# df.idxmax() # 每列最大的值的索引名\n",
    "# df.idxmin() # 最小\n",
    "# df.cummax() # 累积最大值\n",
    "# df.cummin() # 累积最小值\n",
    "# df.skew() # 样本偏度 (第三阶)\n",
    "# df.kurt() # 样本峰度 (第四阶)\n",
    "# df.quantile() # 样本分位数 (不同 % 的值)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d613292",
   "metadata": {},
   "source": [
    "## Pandas求值计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1eb4f15c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:30:41.640011Z",
     "start_time": "2022-05-06T08:30:41.629940Z"
    }
   },
   "outputs": [],
   "source": [
    "# 除了简单的数学统计外，我们往往对数据还需要做非统计性计算，比如去重、格式化等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "5b8338fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:30:42.271505Z",
     "start_time": "2022-05-06T08:30:42.245188Z"
    }
   },
   "outputs": [],
   "source": [
    "# 列的加减乘除\n",
    "df.eval('Q2Q3 = Q2+Q3')  # 列的加减乘除\n",
    "# df.eval('Q2Q3 = Q2+Q3', inplace= True) # 替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e93901d9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:30:42.636811Z",
     "start_time": "2022-05-06T08:30:42.590747Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0      Liver    E  89  21  24  64    45\n",
       "1       Arry    C  36  37  37  57    74\n",
       "2        Ack    A  57  60  18  84    78\n",
       "3      Eorge    C  93  96  71  78   167\n",
       "4        Oah    D  65  49  61  86   110\n",
       "..       ...  ...  ..  ..  ..  ..   ...\n",
       "95   Gabriel    C  48  59  87  74   146\n",
       "96   Austin7    C  21  31  30  43    61\n",
       "97  Lincoln4    C  98  93   1  20    94\n",
       "98       Eli    E  11  74  58  91   132\n",
       "99       Ben    E  21  43  41  74    84\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据指定保留小数位\n",
    "df.round(2) # 指定保留小数位数\n",
    "df.round({'Q1':2, 'Q2':2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "f2300c6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0      Liver    E  89  21  24  64    45\n",
       "1       Arry    C  36  37  37  57    74\n",
       "2        Ack    A  57  60  18  84    78\n",
       "3      Eorge    C  93  96  71  78   167\n",
       "4        Oah    D  65  49  61  86   110\n",
       "..       ...  ...  ..  ..  ..  ..   ...\n",
       "95   Gabriel    C  48  59  87  74   146\n",
       "96   Austin7    C  21  31  30  43    61\n",
       "97  Lincoln4    C  98  93   1  20    94\n",
       "98       Eli    E  11  74  58  91   132\n",
       "99       Ben    E  21  43  41  74    84\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "dc73d04a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:30:42.897729Z",
     "start_time": "2022-05-06T08:30:42.709302Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2], dtype=int64)"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除重复值\n",
    "df.nunique()\n",
    "s.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "07e28712",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:30:43.300060Z",
     "start_time": "2022-05-06T08:30:43.290190Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py:163\u001b[0m, in \u001b[0;36m_na_arithmetic_op\u001b[1;34m(left, right, op, is_cmp)\u001b[0m\n\u001b[0;32m    162\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 163\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mleft\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mright\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    164\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\computation\\expressions.py:239\u001b[0m, in \u001b[0;36mevaluate\u001b[1;34m(op, a, b, use_numexpr)\u001b[0m\n\u001b[0;32m    237\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m use_numexpr:\n\u001b[0;32m    238\u001b[0m         \u001b[38;5;66;03m# error: \"None\" not callable\u001b[39;00m\n\u001b[1;32m--> 239\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_evaluate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_str\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# type: ignore[misc]\u001b[39;00m\n\u001b[0;32m    240\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _evaluate_standard(op, op_str, a, b)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\computation\\expressions.py:128\u001b[0m, in \u001b[0;36m_evaluate_numexpr\u001b[1;34m(op, op_str, a, b)\u001b[0m\n\u001b[0;32m    127\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 128\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43m_evaluate_standard\u001b[49m\u001b[43m(\u001b[49m\u001b[43mop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_str\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    130\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\computation\\expressions.py:69\u001b[0m, in \u001b[0;36m_evaluate_standard\u001b[1;34m(op, op_str, a, b)\u001b[0m\n\u001b[0;32m     68\u001b[0m     _store_test_result(\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m---> 69\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mop\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [207]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# df整体所有元素做加减乘除等计算\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[43mdf\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\ops\\common.py:70\u001b[0m, in \u001b[0;36m_unpack_zerodim_and_defer.<locals>.new_method\u001b[1;34m(self, other)\u001b[0m\n\u001b[0;32m     66\u001b[0m             \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mNotImplemented\u001b[39m\n\u001b[0;32m     68\u001b[0m other \u001b[38;5;241m=\u001b[39m item_from_zerodim(other)\n\u001b[1;32m---> 70\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmethod\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\arraylike.py:100\u001b[0m, in \u001b[0;36mOpsMixin.__add__\u001b[1;34m(self, other)\u001b[0m\n\u001b[0;32m     98\u001b[0m \u001b[38;5;129m@unpack_zerodim_and_defer\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__add__\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     99\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__add__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other):\n\u001b[1;32m--> 100\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_arith_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:6946\u001b[0m, in \u001b[0;36mDataFrame._arith_method\u001b[1;34m(self, other, op)\u001b[0m\n\u001b[0;32m   6942\u001b[0m other \u001b[38;5;241m=\u001b[39m ops\u001b[38;5;241m.\u001b[39mmaybe_prepare_scalar_for_op(other, (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mshape[axis],))\n\u001b[0;32m   6944\u001b[0m \u001b[38;5;28mself\u001b[39m, other \u001b[38;5;241m=\u001b[39m ops\u001b[38;5;241m.\u001b[39malign_method_FRAME(\u001b[38;5;28mself\u001b[39m, other, axis, flex\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, level\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m-> 6946\u001b[0m new_data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_dispatch_frame_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mother\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   6947\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_construct_result(new_data)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:6973\u001b[0m, in \u001b[0;36mDataFrame._dispatch_frame_op\u001b[1;34m(self, right, func, axis)\u001b[0m\n\u001b[0;32m   6970\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_list_like(right):\n\u001b[0;32m   6971\u001b[0m     \u001b[38;5;66;03m# i.e. scalar, faster than checking np.ndim(right) == 0\u001b[39;00m\n\u001b[0;32m   6972\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39merrstate(\u001b[38;5;28mall\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mignore\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m-> 6973\u001b[0m         bm \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43marray_op\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mright\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mright\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   6974\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_constructor(bm)\n\u001b[0;32m   6976\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(right, DataFrame):\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py:302\u001b[0m, in \u001b[0;36mBaseBlockManager.apply\u001b[1;34m(self, f, align_keys, ignore_failures, **kwargs)\u001b[0m\n\u001b[0;32m    300\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    301\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m callable(f):\n\u001b[1;32m--> 302\u001b[0m         applied \u001b[38;5;241m=\u001b[39m b\u001b[38;5;241m.\u001b[39mapply(f, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m    303\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    304\u001b[0m         applied \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(b, f)(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\blocks.py:402\u001b[0m, in \u001b[0;36mBlock.apply\u001b[1;34m(self, func, **kwargs)\u001b[0m\n\u001b[0;32m    396\u001b[0m \u001b[38;5;129m@final\u001b[39m\n\u001b[0;32m    397\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mapply\u001b[39m(\u001b[38;5;28mself\u001b[39m, func, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[Block]:\n\u001b[0;32m    398\u001b[0m     \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    399\u001b[0m \u001b[38;5;124;03m    apply the function to my values; return a block if we are not\u001b[39;00m\n\u001b[0;32m    400\u001b[0m \u001b[38;5;124;03m    one\u001b[39;00m\n\u001b[0;32m    401\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> 402\u001b[0m     result \u001b[38;5;241m=\u001b[39m func(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvalues, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m    404\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_split_op_result(result)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py:222\u001b[0m, in \u001b[0;36marithmetic_op\u001b[1;34m(left, right, op)\u001b[0m\n\u001b[0;32m    217\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    218\u001b[0m     \u001b[38;5;66;03m# TODO we should handle EAs consistently and move this check before the if/else\u001b[39;00m\n\u001b[0;32m    219\u001b[0m     \u001b[38;5;66;03m# (https://github.com/pandas-dev/pandas/issues/41165)\u001b[39;00m\n\u001b[0;32m    220\u001b[0m     _bool_arith_check(op, left, right)\n\u001b[1;32m--> 222\u001b[0m     res_values \u001b[38;5;241m=\u001b[39m \u001b[43m_na_arithmetic_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mleft\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mright\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    224\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m res_values\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py:170\u001b[0m, in \u001b[0;36m_na_arithmetic_op\u001b[1;34m(left, right, op, is_cmp)\u001b[0m\n\u001b[0;32m    164\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m    165\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_cmp \u001b[38;5;129;01mand\u001b[39;00m (is_object_dtype(left\u001b[38;5;241m.\u001b[39mdtype) \u001b[38;5;129;01mor\u001b[39;00m is_object_dtype(right)):\n\u001b[0;32m    166\u001b[0m         \u001b[38;5;66;03m# For object dtype, fallback to a masked operation (only operating\u001b[39;00m\n\u001b[0;32m    167\u001b[0m         \u001b[38;5;66;03m#  on the non-missing values)\u001b[39;00m\n\u001b[0;32m    168\u001b[0m         \u001b[38;5;66;03m# Don't do this for comparisons, as that will handle complex numbers\u001b[39;00m\n\u001b[0;32m    169\u001b[0m         \u001b[38;5;66;03m#  incorrectly, see GH#32047\u001b[39;00m\n\u001b[1;32m--> 170\u001b[0m         result \u001b[38;5;241m=\u001b[39m \u001b[43m_masked_arith_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43mleft\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mright\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    171\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m    172\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\ops\\array_ops.py:127\u001b[0m, in \u001b[0;36m_masked_arith_op\u001b[1;34m(x, y, op)\u001b[0m\n\u001b[0;32m    124\u001b[0m         mask \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mwhere(y \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m, mask)\n\u001b[0;32m    126\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m mask\u001b[38;5;241m.\u001b[39many():\n\u001b[1;32m--> 127\u001b[0m         result[mask] \u001b[38;5;241m=\u001b[39m \u001b[43mop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mxrav\u001b[49m\u001b[43m[\u001b[49m\u001b[43mmask\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    129\u001b[0m np\u001b[38;5;241m.\u001b[39mputmask(result, \u001b[38;5;241m~\u001b[39mmask, np\u001b[38;5;241m.\u001b[39mnan)\n\u001b[0;32m    130\u001b[0m result \u001b[38;5;241m=\u001b[39m result\u001b[38;5;241m.\u001b[39mreshape(x\u001b[38;5;241m.\u001b[39mshape)  \u001b[38;5;66;03m# 2D compat\u001b[39;00m\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "# df整体所有元素做加减乘除等计算\n",
    "\n",
    "# df + 1 # 等运算\n",
    "# df.add() # 加\n",
    "# df.sub() # 减\n",
    "# df.mul() # 乘\n",
    "# df.div() # 除\n",
    "# df.divmod() # 返回 (a // b, a % b)\n",
    "# df.truediv() # Divide DataFrames (float division).\n",
    "# df.floordiv() # Divide DataFrames (integer division).\n",
    "# df.mod() # 模，除后的余数\n",
    "# df.pow() # 指数幂\n",
    "# df.dot(df2) # 矩阵运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba737a35",
   "metadata": {},
   "source": [
    "## Pandas查询筛选数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74095744",
   "metadata": {},
   "source": [
    "### 数据检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "709ac04d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:32:29.202007Z",
     "start_time": "2022-05-06T08:32:29.178666Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "19  Max    E  97  75  41   3   116"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head() # 头几行\n",
    "df.tail() # 尾几行\n",
    "df.sample() # 随机抽样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60d4c4eb",
   "metadata": {},
   "source": [
    "### 操作列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "4159c803",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0      Liver    E  89  21  24  64    45\n",
       "1       Arry    C  36  37  37  57    74\n",
       "2        Ack    A  57  60  18  84    78\n",
       "3      Eorge    C  93  96  71  78   167\n",
       "4        Oah    D  65  49  61  86   110\n",
       "..       ...  ...  ..  ..  ..  ..   ...\n",
       "95   Gabriel    C  48  59  87  74   146\n",
       "96   Austin7    C  21  31  30  43    61\n",
       "97  Lincoln4    C  98  93   1  20    94\n",
       "98       Eli    E  11  74  58  91   132\n",
       "99       Ben    E  21  43  41  74    84\n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "57f4d4cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:39:05.113584Z",
     "start_time": "2022-05-06T08:39:05.088791Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0  Liver    E  89  21  24  64    45\n",
       "1   Arry    C  36  37  37  57    74\n",
       "2    Ack    A  57  60  18  84    78\n",
       "3  Eorge    C  93  96  71  78   167\n",
       "4    Oah    D  65  49  61  86   110"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()  # df=> dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "id": "d033125e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:39:47.617297Z",
     "start_time": "2022-05-06T08:39:47.591385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Liver\n",
       "1         Arry\n",
       "2          Ack\n",
       "3        Eorge\n",
       "4          Oah\n",
       "        ...   \n",
       "95     Gabriel\n",
       "96     Austin7\n",
       "97    Lincoln4\n",
       "98         Eli\n",
       "99         Ben\n",
       "Name: name, Length: 100, dtype: object"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['name']   # 查看指定列，会返回该列的Series\n",
    "# df.name  # 同上"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2768ec3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:39:30.874327Z",
     "start_time": "2022-05-06T08:39:30.862667Z"
    }
   },
   "source": [
    "### 选择部分行列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "e906047d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T08:54:06.266247Z",
     "start_time": "2022-05-06T08:54:06.250396Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n操作\\t语法\\t返回结果\\n选择列\\tdf[col]\\tSeries\\n按索引选择行\\tdf.loc[label]\\tSeries\\n按数字索引选择行\\tdf.iloc[loc]\\tSeries\\n使用切片选择行\\tdf[5:10]\\tDataFrame\\n用表达式筛选行\\tdf[bool_vec]\\tDataFrame\\n'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "操作\t语法\t返回结果\n",
    "选择列\tdf[col]\tSeries\n",
    "按索引选择行\tdf.loc[label]\tSeries\n",
    "按数字索引选择行\tdf.iloc[loc]\tSeries\n",
    "使用切片选择行\tdf[5:10]\tDataFrame\n",
    "用表达式筛选行\tdf[bool_vec]\tDataFrame\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f1677b4",
   "metadata": {},
   "source": [
    "#### 按列切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "b76a807c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T09:28:33.870624Z",
     "start_time": "2022-05-06T09:28:33.839307Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name  Q4\n",
       "0      Liver  64\n",
       "1       Arry  57\n",
       "2        Ack  84\n",
       "3      Eorge  78\n",
       "4        Oah  86\n",
       "..       ...  ..\n",
       "95   Gabriel  74\n",
       "96   Austin7  43\n",
       "97  Lincoln4  20\n",
       "98       Eli  91\n",
       "99       Ben  74\n",
       "\n",
       "[100 rows x 2 columns]"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['name']  # 返回series\n",
    "df[['name']]  # 返回dataframe\n",
    "df[['name', 'Q4']] # 返回多列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f004adab",
   "metadata": {},
   "source": [
    "#### 按行切片.loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "376cef5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=100, step=1)"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "d3d1f979",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    2\n",
       "a    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# # df[:1]\n",
    "# df[:2]  # 返回前两行的数据  [0:2]\n",
    "# df[0:2]\n",
    "# df[4:10] # 返回4-9行的数据\n",
    "# df[:] # 返回所有数据，一般不这么用\n",
    "# df[0:10:2] # 第0到9行，按步长为2进行读取\n",
    "\n",
    "# s[::-1] # 反转顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "453f5628",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T09:22:12.670101Z",
     "start_time": "2022-05-06T09:22:12.623420Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Acob</td>\n",
       "      <td>B</td>\n",
       "      <td>61</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "      <td>8</td>\n",
       "      <td>189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Reddie</td>\n",
       "      <td>D</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>57</td>\n",
       "      <td>72</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Leo</td>\n",
       "      <td>B</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>33</td>\n",
       "      <td>79</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Archie</td>\n",
       "      <td>C</td>\n",
       "      <td>83</td>\n",
       "      <td>89</td>\n",
       "      <td>59</td>\n",
       "      <td>68</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Thomas</td>\n",
       "      <td>B</td>\n",
       "      <td>80</td>\n",
       "      <td>48</td>\n",
       "      <td>56</td>\n",
       "      <td>41</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Joshua</td>\n",
       "      <td>A</td>\n",
       "      <td>63</td>\n",
       "      <td>4</td>\n",
       "      <td>80</td>\n",
       "      <td>30</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>William</td>\n",
       "      <td>C</td>\n",
       "      <td>80</td>\n",
       "      <td>68</td>\n",
       "      <td>3</td>\n",
       "      <td>26</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>Lucas</td>\n",
       "      <td>A</td>\n",
       "      <td>60</td>\n",
       "      <td>41</td>\n",
       "      <td>77</td>\n",
       "      <td>62</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>Arthur</td>\n",
       "      <td>A</td>\n",
       "      <td>44</td>\n",
       "      <td>53</td>\n",
       "      <td>42</td>\n",
       "      <td>40</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>Isaac</td>\n",
       "      <td>E</td>\n",
       "      <td>74</td>\n",
       "      <td>23</td>\n",
       "      <td>28</td>\n",
       "      <td>65</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Teddy</td>\n",
       "      <td>E</td>\n",
       "      <td>71</td>\n",
       "      <td>91</td>\n",
       "      <td>21</td>\n",
       "      <td>48</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>Daniel</td>\n",
       "      <td>C</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>72</td>\n",
       "      <td>61</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>Edward</td>\n",
       "      <td>B</td>\n",
       "      <td>57</td>\n",
       "      <td>38</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Alexander</td>\n",
       "      <td>C</td>\n",
       "      <td>91</td>\n",
       "      <td>76</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>Reggie1</td>\n",
       "      <td>A</td>\n",
       "      <td>30</td>\n",
       "      <td>12</td>\n",
       "      <td>23</td>\n",
       "      <td>9</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Jaxon</td>\n",
       "      <td>E</td>\n",
       "      <td>88</td>\n",
       "      <td>98</td>\n",
       "      <td>19</td>\n",
       "      <td>98</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Elijah</td>\n",
       "      <td>B</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>46</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>Toby</td>\n",
       "      <td>A</td>\n",
       "      <td>52</td>\n",
       "      <td>27</td>\n",
       "      <td>17</td>\n",
       "      <td>68</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Dylan</td>\n",
       "      <td>A</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>65</td>\n",
       "      <td>20</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>Benjamin</td>\n",
       "      <td>D</td>\n",
       "      <td>15</td>\n",
       "      <td>88</td>\n",
       "      <td>52</td>\n",
       "      <td>25</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>Tommy</td>\n",
       "      <td>C</td>\n",
       "      <td>29</td>\n",
       "      <td>44</td>\n",
       "      <td>28</td>\n",
       "      <td>76</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>Louie</td>\n",
       "      <td>D</td>\n",
       "      <td>24</td>\n",
       "      <td>84</td>\n",
       "      <td>54</td>\n",
       "      <td>11</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>Jenson</td>\n",
       "      <td>B</td>\n",
       "      <td>66</td>\n",
       "      <td>77</td>\n",
       "      <td>88</td>\n",
       "      <td>74</td>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>Bobby1</td>\n",
       "      <td>D</td>\n",
       "      <td>50</td>\n",
       "      <td>55</td>\n",
       "      <td>60</td>\n",
       "      <td>59</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>Ollie3</td>\n",
       "      <td>C</td>\n",
       "      <td>10</td>\n",
       "      <td>76</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>David</td>\n",
       "      <td>B</td>\n",
       "      <td>21</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>2</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>Lewis</td>\n",
       "      <td>B</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>77</td>\n",
       "      <td>28</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>Ronnie</td>\n",
       "      <td>B</td>\n",
       "      <td>53</td>\n",
       "      <td>13</td>\n",
       "      <td>34</td>\n",
       "      <td>99</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>Matthew</td>\n",
       "      <td>C</td>\n",
       "      <td>44</td>\n",
       "      <td>33</td>\n",
       "      <td>41</td>\n",
       "      <td>98</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>Harvey2</td>\n",
       "      <td>B</td>\n",
       "      <td>43</td>\n",
       "      <td>76</td>\n",
       "      <td>87</td>\n",
       "      <td>90</td>\n",
       "      <td>163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>Jayden6</td>\n",
       "      <td>D</td>\n",
       "      <td>64</td>\n",
       "      <td>21</td>\n",
       "      <td>10</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>Hunter3</td>\n",
       "      <td>D</td>\n",
       "      <td>38</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>40</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>Nathan</td>\n",
       "      <td>A</td>\n",
       "      <td>87</td>\n",
       "      <td>77</td>\n",
       "      <td>62</td>\n",
       "      <td>13</td>\n",
       "      <td>139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>Luke6</td>\n",
       "      <td>D</td>\n",
       "      <td>15</td>\n",
       "      <td>97</td>\n",
       "      <td>95</td>\n",
       "      <td>99</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>Roman</td>\n",
       "      <td>E</td>\n",
       "      <td>73</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "      <td>44</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>Dexter</td>\n",
       "      <td>E</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>53</td>\n",
       "      <td>20</td>\n",
       "      <td>147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>Elliott</td>\n",
       "      <td>B</td>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>33</td>\n",
       "      <td>60</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>Ryan</td>\n",
       "      <td>E</td>\n",
       "      <td>92</td>\n",
       "      <td>70</td>\n",
       "      <td>64</td>\n",
       "      <td>31</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>Finn</td>\n",
       "      <td>E</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>55</td>\n",
       "      <td>32</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>Kai</td>\n",
       "      <td>B</td>\n",
       "      <td>66</td>\n",
       "      <td>45</td>\n",
       "      <td>13</td>\n",
       "      <td>48</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>Calum</td>\n",
       "      <td>C</td>\n",
       "      <td>14</td>\n",
       "      <td>91</td>\n",
       "      <td>16</td>\n",
       "      <td>82</td>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>Leon</td>\n",
       "      <td>E</td>\n",
       "      <td>38</td>\n",
       "      <td>60</td>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Grayson7</td>\n",
       "      <td>B</td>\n",
       "      <td>59</td>\n",
       "      <td>84</td>\n",
       "      <td>74</td>\n",
       "      <td>33</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>Aiden</td>\n",
       "      <td>D</td>\n",
       "      <td>20</td>\n",
       "      <td>31</td>\n",
       "      <td>62</td>\n",
       "      <td>68</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0       Liver    E  89  21  24  64    45\n",
       "2         Ack    A  57  60  18  84    78\n",
       "4         Oah    D  65  49  61  86   110\n",
       "6        Acob    B  61  95  94   8   189\n",
       "8      Reddie    D  64  93  57  72   150\n",
       "10        Leo    B  17   4  33  79    37\n",
       "12     Archie    C  83  89  59  68   148\n",
       "14     Thomas    B  80  48  56  41   104\n",
       "16     Joshua    A  63   4  80  30    84\n",
       "18    William    C  80  68   3  26    71\n",
       "20      Lucas    A  60  41  77  62   118\n",
       "22     Arthur    A  44  53  42  40    95\n",
       "24      Isaac    E  74  23  28  65    51\n",
       "26      Teddy    E  71  91  21  48   112\n",
       "28     Daniel    C  50  50  72  61   122\n",
       "30     Edward    B  57  38  86  87   124\n",
       "32  Alexander    C  91  76  26  79   102\n",
       "34    Reggie1    A  30  12  23   9    35\n",
       "36      Jaxon    E  88  98  19  98   117\n",
       "38     Elijah    B  97  89  15  46   104\n",
       "40       Toby    A  52  27  17  68    44\n",
       "42      Dylan    A  86  87  65  20   152\n",
       "44   Benjamin    D  15  88  52  25   140\n",
       "46      Tommy    C  29  44  28  76    72\n",
       "48      Louie    D  24  84  54  11   138\n",
       "50     Jenson    B  66  77  88  74   165\n",
       "52     Bobby1    D  50  55  60  59   115\n",
       "54     Ollie3    C  10  76  30  36   106\n",
       "56      David    B  21  47  99   2   146\n",
       "58      Lewis    B   4  34  77  28   111\n",
       "60     Ronnie    B  53  13  34  99    47\n",
       "62    Matthew    C  44  33  41  98    74\n",
       "64    Harvey2    B  43  76  87  90   163\n",
       "66    Jayden6    D  64  21  10  21    31\n",
       "68    Hunter3    D  38  80  82  40   162\n",
       "70     Nathan    A  87  77  62  13   139\n",
       "72      Luke6    D  15  97  95  99   192\n",
       "74      Roman    E  73   1  25  44    26\n",
       "76     Dexter    E  73  94  53  20   147\n",
       "78    Elliott    B   9  31  33  60    64\n",
       "80       Ryan    E  92  70  64  31   134\n",
       "82       Finn    E   4   1  55  32    56\n",
       "84        Kai    B  66  45  13  48    58\n",
       "86      Calum    C  14  91  16  82   107\n",
       "88      Aaron    A  96  75  55   8   130\n",
       "90       Leon    E  38  60  31   7    91\n",
       "92   Grayson7    B  59  84  74  33   158\n",
       "94      Aiden    D  20  31  62  68    93\n",
       "96    Austin7    C  21  31  30  43    61\n",
       "98        Eli    E  11  74  58  91   132"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.loc[<索引表达式>, <列表达式>]\n",
    "# df.loc[[8]] # 索引单行，选择索引为8的行\n",
    "# df.loc[[0,5,10]] # 索引多行，选择索引为0，5，10的行\n",
    "# df.loc[0:5] # 索引连续的多行，选择0，1，2，3，4,5行\n",
    "# df.loc[np.r_[0:10, 15:20]]  # 索引不连续的多行 0-9， 15-19\n",
    "\n",
    "# # 一行隔一行显示,为真的行显示\n",
    "# df.loc[[False,True]*50]  # 显示1，3，5，7，9.....\n",
    "df.loc[[True,False]*50]  # 显示0，2，4，6，8...."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "103ad9ac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T09:23:43.366499Z",
     "start_time": "2022-05-06T09:23:43.338681Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"None of [Index(['Liver', 'Arry'], dtype='object')] are in the [index]\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [231]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 如果索引是name，\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mLiver\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mArry\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py:967\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m    964\u001b[0m axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maxis \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m    966\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[1;32m--> 967\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_axis\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmaybe_callable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py:1191\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1188\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(key, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m   1189\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot index with multidimensional key\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 1191\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_getitem_iterable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1193\u001b[0m \u001b[38;5;66;03m# nested tuple slicing\u001b[39;00m\n\u001b[0;32m   1194\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_nested_tuple(key, labels):\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py:1132\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_iterable\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1129\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[0;32m   1131\u001b[0m \u001b[38;5;66;03m# A collection of keys\u001b[39;00m\n\u001b[1;32m-> 1132\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_listlike_indexer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1133\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_reindex_with_indexers(\n\u001b[0;32m   1134\u001b[0m     {axis: [keyarr, indexer]}, copy\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, allow_dups\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m   1135\u001b[0m )\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py:1327\u001b[0m, in \u001b[0;36m_LocIndexer._get_listlike_indexer\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1324\u001b[0m ax \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis(axis)\n\u001b[0;32m   1325\u001b[0m axis_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis_name(axis)\n\u001b[1;32m-> 1327\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[43max\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_indexer_strict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1329\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m keyarr, indexer\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py:5782\u001b[0m, in \u001b[0;36mIndex._get_indexer_strict\u001b[1;34m(self, key, axis_name)\u001b[0m\n\u001b[0;32m   5779\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   5780\u001b[0m     keyarr, indexer, new_indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reindex_non_unique(keyarr)\n\u001b[1;32m-> 5782\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_raise_if_missing\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkeyarr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   5784\u001b[0m keyarr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtake(indexer)\n\u001b[0;32m   5785\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Index):\n\u001b[0;32m   5786\u001b[0m     \u001b[38;5;66;03m# GH 42790 - Preserve name from an Index\u001b[39;00m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py:5842\u001b[0m, in \u001b[0;36mIndex._raise_if_missing\u001b[1;34m(self, key, indexer, axis_name)\u001b[0m\n\u001b[0;32m   5840\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m use_interval_msg:\n\u001b[0;32m   5841\u001b[0m         key \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(key)\n\u001b[1;32m-> 5842\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] are in the [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00maxis_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m]\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m   5844\u001b[0m not_found \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(ensure_index(key)[missing_mask\u001b[38;5;241m.\u001b[39mnonzero()[\u001b[38;5;241m0\u001b[39m]]\u001b[38;5;241m.\u001b[39munique())\n\u001b[0;32m   5845\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnot_found\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m not in index\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: \"None of [Index(['Liver', 'Arry'], dtype='object')] are in the [index]\""
     ]
    }
   ],
   "source": [
    "# 如果索引是name，\n",
    "# df.loc[['Liver','Arry']]\n",
    "# df.loc[['Liver': 'Oah']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21cb257c",
   "metadata": {},
   "source": [
    "#### 按行和列切片.loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "2e41baa6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T00:34:32.553514Z",
     "start_time": "2022-05-07T00:34:32.516910Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>24</td>\n",
       "      <td>13</td>\n",
       "      <td>87</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>61</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>99</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>57</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>77</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>33</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Q1  Q2  Q3  Q4\n",
       "0   89  21  24  64\n",
       "1   36  37  37  57\n",
       "2   57  60  18  84\n",
       "3   93  96  71  78\n",
       "4   65  49  61  86\n",
       "5   24  13  87  43\n",
       "6   61  95  94   8\n",
       "7    9  10  99  37\n",
       "8   64  93  57  72\n",
       "9   77   9  26  67\n",
       "10  17   4  33  79"
      ]
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.loc[:,['Q1','Q2']]   # Q1,Q2列，所有行\n",
    "# df.loc[:,'Q1':'Q4']   # Q1,Q2,Q3,Q4列，所有行\n",
    "# df.loc[0:10,['Q1','Q4']]   # Q1,Q4列，0到10行\n",
    "df.loc[0:10,'Q1':'Q4']   # Q1,Q2,Q3,Q4列，0到10行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "ef89abf3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T09:41:52.504982Z",
     "start_time": "2022-05-06T09:41:52.472175Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Harlie</td>\n",
       "      <td>C</td>\n",
       "      <td>24</td>\n",
       "      <td>13</td>\n",
       "      <td>87</td>\n",
       "      <td>43</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Acob</td>\n",
       "      <td>B</td>\n",
       "      <td>61</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "      <td>8</td>\n",
       "      <td>189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Lfie</td>\n",
       "      <td>A</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>99</td>\n",
       "      <td>37</td>\n",
       "      <td>109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Reddie</td>\n",
       "      <td>D</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>57</td>\n",
       "      <td>72</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Oscar</td>\n",
       "      <td>A</td>\n",
       "      <td>77</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>67</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>James</td>\n",
       "      <td>E</td>\n",
       "      <td>48</td>\n",
       "      <td>77</td>\n",
       "      <td>52</td>\n",
       "      <td>11</td>\n",
       "      <td>129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>Joshua</td>\n",
       "      <td>A</td>\n",
       "      <td>63</td>\n",
       "      <td>4</td>\n",
       "      <td>80</td>\n",
       "      <td>30</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>William</td>\n",
       "      <td>C</td>\n",
       "      <td>80</td>\n",
       "      <td>68</td>\n",
       "      <td>3</td>\n",
       "      <td>26</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0     Liver    E  89  21  24  64    45\n",
       "1      Arry    C  36  37  37  57    74\n",
       "2       Ack    A  57  60  18  84    78\n",
       "3     Eorge    C  93  96  71  78   167\n",
       "4       Oah    D  65  49  61  86   110\n",
       "5    Harlie    C  24  13  87  43   100\n",
       "6      Acob    B  61  95  94   8   189\n",
       "7      Lfie    A   9  10  99  37   109\n",
       "8    Reddie    D  64  93  57  72   150\n",
       "9     Oscar    A  77   9  26  67    35\n",
       "15    James    E  48  77  52  11   129\n",
       "16   Joshua    A  63   4  80  30    84\n",
       "17    Henry    A  91  15  75  17    90\n",
       "18  William    C  80  68   3  26    71\n",
       "19      Max    E  97  75  41   3   116"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[np.r_[0:10, 15:20]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "766d2d4d",
   "metadata": {},
   "source": [
    "### 取具体某一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "46ba607f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T00:39:13.809616Z",
     "start_time": "2022-05-07T00:39:13.803737Z"
    }
   },
   "outputs": [],
   "source": [
    "# .at[<索引>,<列名>]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "c315c27d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T00:47:46.832846Z",
     "start_time": "2022-05-07T00:47:46.804846Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Liver'"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.at[4,'Q1']   # 索引为4， Q1列\n",
    "# df.at['lily','Q1'] # 假设索引为name， 取lily行，Q1列\n",
    "\n",
    "# .loc和.at联合使用\n",
    "df.loc[0].at['name']  # 0行，name列\n",
    "\n",
    "# # index, .at联合使用\n",
    "# df.set_index('name').at['Eorge','team']  # 设置name为索引，取（Rorge, team）值\n",
    "# df.set_index('name').team.at['Eorge'] \n",
    "\n",
    "# # 指定列，根据索引取值\n",
    "# df.team.at[3]  # 指定team列，取索引3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "7625b694",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T00:41:37.097847Z",
     "start_time": "2022-05-07T00:41:37.086448Z"
    }
   },
   "outputs": [],
   "source": [
    "# .iat\n",
    "# .iat和iloc一样，只支持数字索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d86de829",
   "metadata": {},
   "source": [
    "### 表达式筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "c694ac6e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T01:02:55.938442Z",
     "start_time": "2022-05-07T01:02:55.836127Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Reddie</td>\n",
       "      <td>D</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>57</td>\n",
       "      <td>72</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Logan</td>\n",
       "      <td>B</td>\n",
       "      <td>9</td>\n",
       "      <td>89</td>\n",
       "      <td>35</td>\n",
       "      <td>65</td>\n",
       "      <td>124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Archie</td>\n",
       "      <td>C</td>\n",
       "      <td>83</td>\n",
       "      <td>89</td>\n",
       "      <td>59</td>\n",
       "      <td>68</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Mason</td>\n",
       "      <td>D</td>\n",
       "      <td>80</td>\n",
       "      <td>96</td>\n",
       "      <td>26</td>\n",
       "      <td>49</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Teddy</td>\n",
       "      <td>E</td>\n",
       "      <td>71</td>\n",
       "      <td>91</td>\n",
       "      <td>21</td>\n",
       "      <td>48</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Joseph</td>\n",
       "      <td>E</td>\n",
       "      <td>67</td>\n",
       "      <td>87</td>\n",
       "      <td>87</td>\n",
       "      <td>93</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Jaxon</td>\n",
       "      <td>E</td>\n",
       "      <td>88</td>\n",
       "      <td>98</td>\n",
       "      <td>19</td>\n",
       "      <td>98</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Harley</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>Jude</td>\n",
       "      <td>E</td>\n",
       "      <td>8</td>\n",
       "      <td>45</td>\n",
       "      <td>13</td>\n",
       "      <td>65</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>Tommy</td>\n",
       "      <td>C</td>\n",
       "      <td>29</td>\n",
       "      <td>44</td>\n",
       "      <td>28</td>\n",
       "      <td>76</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>Ollie3</td>\n",
       "      <td>C</td>\n",
       "      <td>10</td>\n",
       "      <td>76</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>Zachary</td>\n",
       "      <td>E</td>\n",
       "      <td>12</td>\n",
       "      <td>71</td>\n",
       "      <td>85</td>\n",
       "      <td>93</td>\n",
       "      <td>156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>Albie1</td>\n",
       "      <td>D</td>\n",
       "      <td>79</td>\n",
       "      <td>82</td>\n",
       "      <td>56</td>\n",
       "      <td>96</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>Jackson5</td>\n",
       "      <td>E</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>33</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>Alex</td>\n",
       "      <td>D</td>\n",
       "      <td>14</td>\n",
       "      <td>70</td>\n",
       "      <td>55</td>\n",
       "      <td>87</td>\n",
       "      <td>125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>Harvey2</td>\n",
       "      <td>B</td>\n",
       "      <td>43</td>\n",
       "      <td>76</td>\n",
       "      <td>87</td>\n",
       "      <td>90</td>\n",
       "      <td>163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>Luke6</td>\n",
       "      <td>D</td>\n",
       "      <td>15</td>\n",
       "      <td>97</td>\n",
       "      <td>95</td>\n",
       "      <td>99</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>Stanley</td>\n",
       "      <td>A</td>\n",
       "      <td>69</td>\n",
       "      <td>71</td>\n",
       "      <td>39</td>\n",
       "      <td>97</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>Elliott</td>\n",
       "      <td>B</td>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>33</td>\n",
       "      <td>60</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>Liam</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "      <td>80</td>\n",
       "      <td>24</td>\n",
       "      <td>25</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>Calum</td>\n",
       "      <td>C</td>\n",
       "      <td>14</td>\n",
       "      <td>91</td>\n",
       "      <td>16</td>\n",
       "      <td>82</td>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>Aiden</td>\n",
       "      <td>D</td>\n",
       "      <td>20</td>\n",
       "      <td>31</td>\n",
       "      <td>62</td>\n",
       "      <td>68</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "1       Arry    C  36  37  37  57    74\n",
       "2        Ack    A  57  60  18  84    78\n",
       "3      Eorge    C  93  96  71  78   167\n",
       "8     Reddie    D  64  93  57  72   150\n",
       "11     Logan    B   9  89  35  65   124\n",
       "12    Archie    C  83  89  59  68   148\n",
       "23     Mason    D  80  96  26  49   122\n",
       "26     Teddy    E  71  91  21  48   112\n",
       "31    Joseph    E  67  87  87  93   174\n",
       "36     Jaxon    E  88  98  19  98   117\n",
       "39    Harley    B   2  99  12  13   111\n",
       "43      Jude    E   8  45  13  65    58\n",
       "46     Tommy    C  29  44  28  76    72\n",
       "54    Ollie3    C  10  76  30  36   106\n",
       "55   Zachary    E  12  71  85  93   156\n",
       "57    Albie1    D  79  82  56  96   138\n",
       "61  Jackson5    E   6  10  15  33    25\n",
       "63      Alex    D  14  70  55  87   125\n",
       "64   Harvey2    B  43  76  87  90   163\n",
       "72     Luke6    D  15  97  95  99   192\n",
       "75   Stanley    A  69  71  39  97   110\n",
       "78   Elliott    B   9  31  33  60    64\n",
       "85      Liam    B   2  80  24  25   104\n",
       "86     Calum    C  14  91  16  82   107\n",
       "94     Aiden    D  20  31  62  68    93\n",
       "96   Austin7    C  21  31  30  43    61\n",
       "98       Eli    E  11  74  58  91   132\n",
       "99       Ben    E  21  43  41  74    84"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# [] 切片里使用表达式进行筛选\n",
    "# 等于\n",
    "# df[df['Q1']==80]  #筛选Q1=8 的行\n",
    "# df[df.Q1==8] #筛选Q1=8 的行\n",
    "\n",
    "# #不等于\n",
    "# df[~(df['Q1']==80)]  # 筛选Q1=！8的行\n",
    "\n",
    "# 大于\n",
    "# df[df['Q1']>90]  # 取Q1大于90的所有列\n",
    "# df.loc[df['Q1']>90,'Q1':] # 取Q1大于90， 列Q1之后所有的列\n",
    "\n",
    "# # &和|\n",
    "# df.loc[(df['Q1']>80) & (df['Q2']<15)] #  Q1>80,Q2<15的数据\n",
    "# df.loc[(df['Q1']>90) | (df['Q2']<90)] #  Q1>90,Q2<90的数据\n",
    "\n",
    "# # 两个列进行对比\n",
    "df[(df['Q1']< df['Q2']) & (df['Q3']< df['Q4']) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "7a8861c3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T01:06:59.406688Z",
     "start_time": "2022-05-07T01:06:59.365876Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Alexander</td>\n",
       "      <td>C</td>\n",
       "      <td>91</td>\n",
       "      <td>76</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Elijah</td>\n",
       "      <td>B</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>46</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>Ryan</td>\n",
       "      <td>E</td>\n",
       "      <td>92</td>\n",
       "      <td>70</td>\n",
       "      <td>64</td>\n",
       "      <td>31</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "17      Henry    A  91  15  75  17    90\n",
       "19        Max    E  97  75  41   3   116\n",
       "32  Alexander    C  91  76  26  79   102\n",
       "38     Elijah    B  97  89  15  46   104\n",
       "80       Ryan    E  92  70  64  31   134\n",
       "88      Aaron    A  96  75  55   8   130"
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .loc里的索引部分可以用表达式进行数据筛选\n",
    "\n",
    "# df.loc[df['Q1']==8]\n",
    "\n",
    "# df.loc[df['Q1']>90,'Q1']   # 返回series\n",
    "# df.loc[df['Q1']>90,['Q1']]  # 返回dataframe\n",
    "\n",
    "# 其他表达式和切片[] 一样\n",
    "\n",
    "# 也可以使用已定义的逻辑判断和函数进行筛选：\n",
    "\"\"\"\n",
    "df.eq() # 等于相等 ==\n",
    "df.ne() # 不等于 !=\n",
    "df.le() # 小于等于 >=\n",
    "df.lt() # 小于 <\n",
    "df.ge() # 大于等于 >=\n",
    "df.gt() # 大于 >\n",
    "\"\"\"\n",
    "# 都支持  axis{0 or ‘index’, 1 or ‘columns’}, default ‘columns’\n",
    "# df[df.Q1.ne(80)] # Q1 不等于8\n",
    "df.loc[df.Q1.gt(90) & df.Q2.lt(90)] # and 关系 Q1>90 Q2<90"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "943968fb",
   "metadata": {},
   "source": [
    "### 函数筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "42f89e76",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T01:14:51.951723Z",
     "start_time": "2022-05-07T01:14:51.923418Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>8</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Q1  Q2\n",
       "43   8  45\n",
       "45   8  12"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数生成具体的标%alias或者同长度对应的布尔索引，作用于筛选\n",
    "df[lambda df:df['Q1'] == 8]\n",
    "\n",
    "df.loc[lambda df:df['Q1'] == 8, 'Q1':'Q2']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ded1c3f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T01:36:54.588775Z",
     "start_time": "2022-05-07T01:36:54.573539Z"
    }
   },
   "source": [
    "### 通过条件判断，替换df中的数据，where 和 mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "59bb78a4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T02:12:16.556231Z",
     "start_time": "2022-05-07T02:12:16.511576Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>-64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36</td>\n",
       "      <td>-37</td>\n",
       "      <td>-37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>-71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-65</td>\n",
       "      <td>-49</td>\n",
       "      <td>-61</td>\n",
       "      <td>-86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>48</td>\n",
       "      <td>-59</td>\n",
       "      <td>87</td>\n",
       "      <td>-74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>21</td>\n",
       "      <td>-31</td>\n",
       "      <td>30</td>\n",
       "      <td>-43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>-98</td>\n",
       "      <td>93</td>\n",
       "      <td>-1</td>\n",
       "      <td>-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>-11</td>\n",
       "      <td>-74</td>\n",
       "      <td>-58</td>\n",
       "      <td>-91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>21</td>\n",
       "      <td>-43</td>\n",
       "      <td>-41</td>\n",
       "      <td>-74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Q1  Q2  Q3  Q4\n",
       "0  -89  21  24 -64\n",
       "1   36 -37 -37  57\n",
       "2   57  60  18  84\n",
       "3   93  96 -71  78\n",
       "4  -65 -49 -61 -86\n",
       "..  ..  ..  ..  ..\n",
       "95  48 -59  87 -74\n",
       "96  21 -31  30 -43\n",
       "97 -98  93  -1 -20\n",
       "98 -11 -74 -58 -91\n",
       "99  21 -43 -41 -74\n",
       "\n",
       "[100 rows x 4 columns]"
      ]
     },
     "execution_count": 249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# s.where(s>90) # 将不满足>90条件的数据替换成NAN\n",
    "# s.where(s>90,0) # 将不满足>90条件的数据替换成0\n",
    "\n",
    "# np.where(s>80,True,False) \n",
    "# np.where(s>=60,'及格','不及格') \n",
    "\n",
    "\n",
    "# s.mask(s>90) # 将满足>90条件的数据替换成NAN\n",
    "# s.mask(s>90,0) # 将满足>90条件的数据替换成0\n",
    "\n",
    "# 通过where mask判断，给df重新赋值\n",
    "\n",
    "# 能被整除的显示，不能的显示相反数\n",
    "# m = df.loc[:,'Q1':'Q4']%3 == 0\n",
    "df.loc[:,'Q1':'Q4'].where(m, -df.loc[:,'Q1':'Q4'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b28a2396",
   "metadata": {},
   "source": [
    "### Query,写SQL里的where查询语言"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "27d9c54d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T02:24:31.270752Z",
     "start_time": "2022-05-07T02:24:31.154014Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49.2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Alexander</td>\n",
       "      <td>C</td>\n",
       "      <td>91</td>\n",
       "      <td>76</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Adam</td>\n",
       "      <td>C</td>\n",
       "      <td>90</td>\n",
       "      <td>32</td>\n",
       "      <td>47</td>\n",
       "      <td>39</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Elijah</td>\n",
       "      <td>B</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>46</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>Ryan</td>\n",
       "      <td>E</td>\n",
       "      <td>92</td>\n",
       "      <td>70</td>\n",
       "      <td>64</td>\n",
       "      <td>31</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "3       Eorge    C  93  96  71  78   167\n",
       "17      Henry    A  91  15  75  17    90\n",
       "19        Max    E  97  75  41   3   116\n",
       "32  Alexander    C  91  76  26  79   102\n",
       "33       Adam    C  90  32  47  39    79\n",
       "38     Elijah    B  97  89  15  46   104\n",
       "80       Ryan    E  92  70  64  31   134\n",
       "88      Aaron    A  96  75  55   8   130\n",
       "97   Lincoln4    C  98  93   1  20    94"
      ]
     },
     "execution_count": 257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.query('Q1 > Q2 > 90') # 直接写类型 sql where 语句\n",
    "# df.query('Q1 + Q2 > 180')\n",
    "# df.query('Q1 == Q2')\n",
    "# df.query('(Q1<50) & (Q2>40) and (Q3>90)')\n",
    "# df.query('Q1 > Q2 > Q3 > Q4')\n",
    "\n",
    "# df.query('team == \"C\"')\n",
    "# df.query('team in [\"A\",\"B\"]')\n",
    "# df.query('team not in (\"E\",\"A\",\"B\")')\n",
    "# df.query('team == [\"A\",\"B\"]')\n",
    "# df.query('team != [\"A\",\"B\"]')\n",
    "# df.query('name.str.contains(\"am\")') # 包含 am 字符\n",
    "\n",
    "# # 对于名称中带有空格的列，可以使用反引号引起来 \n",
    "# # df.query('`team name` == B')\n",
    "\n",
    "# # 支持传入变量，如：大于平均分40分的\n",
    "# a = df.Q1.mean()\n",
    "# df.query('Q1 > @a+40')\n",
    "# df.query('Q1 > `Q2`+@a')\n",
    "\n",
    "# # df.eval() 用法与 df.query 类似\n",
    "# df[df.eval(\"Q1 > 90 > Q3 > 10\")]\n",
    "# df[df.eval(\"Q1 > `Q2`+@a\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d20d603f",
   "metadata": {},
   "source": [
    "### Filter, 对行名和列名进行筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "3511891a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T02:50:55.432486Z",
     "start_time": "2022-05-07T02:50:55.383993Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Q1  Q2  Q3  Q4  Q2Q3\n",
       "0   89  21  24  64    45\n",
       "1   36  37  37  57    74\n",
       "2   57  60  18  84    78\n",
       "3   93  96  71  78   167\n",
       "4   65  49  61  86   110\n",
       "..  ..  ..  ..  ..   ...\n",
       "95  48  59  87  74   146\n",
       "96  21  31  30  43    61\n",
       "97  98  93   1  20    94\n",
       "98  11  74  58  91   132\n",
       "99  21  43  41  74    84\n",
       "\n",
       "[100 rows x 5 columns]"
      ]
     },
     "execution_count": 261,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.filter(items=['Q1','Q2'])  # 等同于df[['Q1','Q2']]\n",
    "\n",
    "\n",
    "# df.filter(regex='Q',axis=1) # 列名包含Q的\n",
    "\n",
    "# # 使用正则表达式\n",
    "# df.filter(regex='e$',axis=1) # 列名以e结尾的\n",
    "# df.filter(regex='^Q',axis=1) # 列名以Q开头的\n",
    "# df.filter(regex='1$',axis=0) # 索引中以1结尾的\n",
    "\n",
    "# df.filter(like='2',axis=0) # 索引中包含2的\n",
    "\n",
    "# df.filter(regex='^2',axis=0).filter(like='Q',axis=1)  # 筛选出2开头的行，包含Q的列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd96df77",
   "metadata": {},
   "source": [
    "### take 延轴返回指定数字索引中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "id": "b8e655c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T02:55:58.171386Z",
     "start_time": "2022-05-07T02:55:58.143386Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "2    Ack    A  57  60  18  84    78\n",
       "3  Eorge    C  93  96  71  78   167"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.take([2,21,5])  # 取指定数字索引的行数据，不指定axis，默认是行函数\n",
    "df.take([2,3],axis=1) # 取指定数字索引的列数据\n",
    "# df.take([2,3],axis=0) \n",
    "# df.take([-1,-2]) # 索引的反向指定"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e7e0d30",
   "metadata": {},
   "source": [
    "### pd.IndexSlice索引选择器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "0bbf7e8d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T03:00:54.626251Z",
     "start_time": "2022-05-07T03:00:54.543603Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Q1  Q2\n",
       "0   89  21\n",
       "1   36  37\n",
       "2   57  60\n",
       "3   93  96\n",
       "4   65  49\n",
       "..  ..  ..\n",
       "95  48  59\n",
       "96  21  31\n",
       "97  98  93\n",
       "98  11  74\n",
       "99  21  43\n",
       "\n",
       "[100 rows x 2 columns]"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pd.IndexSlice 的使用方法类似于df.loc[] 切片中的方法，常用在多层索引中，以及需要指定应用范围（subset 参数）的函数中，特别是在链式方法中。\n",
    "\n",
    "df.loc[pd.IndexSlice[:, ['Q1', 'Q2']]]\n",
    "# 变量化使用\n",
    "idx = pd.IndexSlice\n",
    "df.loc[idx[:, ['Q1', 'Q2']]]\n",
    "df.loc[idx[:, 'Q1':'Q4'], :] # 多索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "c3a14e42",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T03:01:14.980806Z",
     "start_time": "2022-05-07T03:01:14.956165Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'style_fun' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [102]\u001b[0m, in \u001b[0;36m<cell line: 9>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      7\u001b[0m df\u001b[38;5;241m.\u001b[39mloc[idxs]\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 选择这部分区域加样式（样式功能见教程后文介绍）\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m df\u001b[38;5;241m.\u001b[39mstyle\u001b[38;5;241m.\u001b[39mapplymap(\u001b[43mstyle_fun\u001b[49m, subset\u001b[38;5;241m=\u001b[39midxs)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'style_fun' is not defined"
     ]
    }
   ],
   "source": [
    "# 复杂的选择：\n",
    "\n",
    "# 创建复杂条件选择器\n",
    "selected = df.loc[(df.team=='A') & (df.Q1>90)]\n",
    "idxs = pd.IndexSlice[selected.index, 'name']\n",
    "# 应用选择器\n",
    "df.loc[idxs]\n",
    "# 选择这部分区域加样式（样式功能见教程后文介绍）\n",
    "df.style.applymap(style_fun, subset=idxs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52b7b180",
   "metadata": {},
   "source": [
    "### select_dtype 按数据类型筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b69f674",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T03:03:43.846684Z",
     "start_time": "2022-05-07T03:03:43.815462Z"
    }
   },
   "outputs": [],
   "source": [
    "df.select_dtypes(include=['float64'])  # 选择float64型数据\n",
    "df.select_dtypes(include='bool')\n",
    "df.select_dtypes(include=['number'])\n",
    "df.select_dtypes(exclude=['int']) # 排除int类型\n",
    "df.select_dtypes(exclude=['datetime64'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e2bd79f",
   "metadata": {},
   "source": [
    "### any和all 进行判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "id": "7e094cd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T03:17:16.057326Z",
     "start_time": "2022-05-07T03:17:15.982456Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>Q2Q3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Acob</td>\n",
       "      <td>B</td>\n",
       "      <td>61</td>\n",
       "      <td>95</td>\n",
       "      <td>94</td>\n",
       "      <td>8</td>\n",
       "      <td>189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Reddie</td>\n",
       "      <td>D</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>57</td>\n",
       "      <td>72</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Logan</td>\n",
       "      <td>B</td>\n",
       "      <td>9</td>\n",
       "      <td>89</td>\n",
       "      <td>35</td>\n",
       "      <td>65</td>\n",
       "      <td>124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Archie</td>\n",
       "      <td>C</td>\n",
       "      <td>83</td>\n",
       "      <td>89</td>\n",
       "      <td>59</td>\n",
       "      <td>68</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Theo</td>\n",
       "      <td>C</td>\n",
       "      <td>51</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>27</td>\n",
       "      <td>173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>Mason</td>\n",
       "      <td>D</td>\n",
       "      <td>80</td>\n",
       "      <td>96</td>\n",
       "      <td>26</td>\n",
       "      <td>49</td>\n",
       "      <td>122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>Harrison</td>\n",
       "      <td>B</td>\n",
       "      <td>89</td>\n",
       "      <td>13</td>\n",
       "      <td>18</td>\n",
       "      <td>75</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>Teddy</td>\n",
       "      <td>E</td>\n",
       "      <td>71</td>\n",
       "      <td>91</td>\n",
       "      <td>21</td>\n",
       "      <td>48</td>\n",
       "      <td>112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>Joseph</td>\n",
       "      <td>E</td>\n",
       "      <td>67</td>\n",
       "      <td>87</td>\n",
       "      <td>87</td>\n",
       "      <td>93</td>\n",
       "      <td>174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Alexander</td>\n",
       "      <td>C</td>\n",
       "      <td>91</td>\n",
       "      <td>76</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Adam</td>\n",
       "      <td>C</td>\n",
       "      <td>90</td>\n",
       "      <td>32</td>\n",
       "      <td>47</td>\n",
       "      <td>39</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>Jaxon</td>\n",
       "      <td>E</td>\n",
       "      <td>88</td>\n",
       "      <td>98</td>\n",
       "      <td>19</td>\n",
       "      <td>98</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Elijah</td>\n",
       "      <td>B</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>46</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>Harley</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Dylan</td>\n",
       "      <td>A</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>65</td>\n",
       "      <td>20</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>Benjamin</td>\n",
       "      <td>D</td>\n",
       "      <td>15</td>\n",
       "      <td>88</td>\n",
       "      <td>52</td>\n",
       "      <td>25</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>Louie</td>\n",
       "      <td>D</td>\n",
       "      <td>24</td>\n",
       "      <td>84</td>\n",
       "      <td>54</td>\n",
       "      <td>11</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>Albie1</td>\n",
       "      <td>D</td>\n",
       "      <td>79</td>\n",
       "      <td>82</td>\n",
       "      <td>56</td>\n",
       "      <td>96</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>Nathan</td>\n",
       "      <td>A</td>\n",
       "      <td>87</td>\n",
       "      <td>77</td>\n",
       "      <td>62</td>\n",
       "      <td>13</td>\n",
       "      <td>139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>Luke6</td>\n",
       "      <td>D</td>\n",
       "      <td>15</td>\n",
       "      <td>97</td>\n",
       "      <td>95</td>\n",
       "      <td>99</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>Dexter</td>\n",
       "      <td>E</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>53</td>\n",
       "      <td>20</td>\n",
       "      <td>147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>Michael</td>\n",
       "      <td>B</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>59</td>\n",
       "      <td>92</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>Ryan</td>\n",
       "      <td>E</td>\n",
       "      <td>92</td>\n",
       "      <td>70</td>\n",
       "      <td>64</td>\n",
       "      <td>31</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>Albert0</td>\n",
       "      <td>B</td>\n",
       "      <td>85</td>\n",
       "      <td>38</td>\n",
       "      <td>41</td>\n",
       "      <td>17</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>Calum</td>\n",
       "      <td>C</td>\n",
       "      <td>14</td>\n",
       "      <td>91</td>\n",
       "      <td>16</td>\n",
       "      <td>82</td>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>Louis2</td>\n",
       "      <td>C</td>\n",
       "      <td>13</td>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>22</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Grayson7</td>\n",
       "      <td>B</td>\n",
       "      <td>59</td>\n",
       "      <td>84</td>\n",
       "      <td>74</td>\n",
       "      <td>33</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>Jamie0</td>\n",
       "      <td>B</td>\n",
       "      <td>39</td>\n",
       "      <td>97</td>\n",
       "      <td>84</td>\n",
       "      <td>55</td>\n",
       "      <td>181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2  Q3  Q4  Q2Q3\n",
       "0       Liver    E  89  21  24  64    45\n",
       "3       Eorge    C  93  96  71  78   167\n",
       "6        Acob    B  61  95  94   8   189\n",
       "8      Reddie    D  64  93  57  72   150\n",
       "11      Logan    B   9  89  35  65   124\n",
       "12     Archie    C  83  89  59  68   148\n",
       "13       Theo    C  51  86  87  27   173\n",
       "17      Henry    A  91  15  75  17    90\n",
       "19        Max    E  97  75  41   3   116\n",
       "23      Mason    D  80  96  26  49   122\n",
       "25   Harrison    B  89  13  18  75    31\n",
       "26      Teddy    E  71  91  21  48   112\n",
       "31     Joseph    E  67  87  87  93   174\n",
       "32  Alexander    C  91  76  26  79   102\n",
       "33       Adam    C  90  32  47  39    79\n",
       "36      Jaxon    E  88  98  19  98   117\n",
       "38     Elijah    B  97  89  15  46   104\n",
       "39     Harley    B   2  99  12  13   111\n",
       "42      Dylan    A  86  87  65  20   152\n",
       "44   Benjamin    D  15  88  52  25   140\n",
       "48      Louie    D  24  84  54  11   138\n",
       "57     Albie1    D  79  82  56  96   138\n",
       "70     Nathan    A  87  77  62  13   139\n",
       "72      Luke6    D  15  97  95  99   192\n",
       "76     Dexter    E  73  94  53  20   147\n",
       "77    Michael    B  89  21  59  92    80\n",
       "80       Ryan    E  92  70  64  31   134\n",
       "83    Albert0    B  85  38  41  17    79\n",
       "86      Calum    C  14  91  16  82   107\n",
       "87     Louis2    C  13  94  51  22   145\n",
       "88      Aaron    A  96  75  55   8   130\n",
       "92   Grayson7    B  59  84  74  33   158\n",
       "93     Jamie0    B  39  97  84  55   181\n",
       "97   Lincoln4    C  98  93   1  20    94"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# any 方法如果至少有一个值为 True 是便为 True，all 需要所有值为 True 才为 True。它们可以传入 axis 为 1，会按行检测。\n",
    "# df[(df.loc[:,['Q1','Q2']]>80).all(1)]  # Q1,Q2成绩全>80分的\n",
    "# df[(df.loc[:,['Q1','Q2']]>80).any(1)]  # Q1,Q2成绩任意一门大于80天"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd6c6a2",
   "metadata": {},
   "source": [
    "## Pandas数据类型的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86a32326",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:38:28.189675Z",
     "start_time": "2022-05-07T06:38:28.183320Z"
    }
   },
   "outputs": [],
   "source": [
    "# 数据分析前，我们需要对数据分配好数据类型，这才能高效的处理数据，不同的数据类型可以用不同的处理方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86588241",
   "metadata": {},
   "source": [
    "### 数据初始化时指定数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5668b0b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a1b7ee65",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:43:45.133803Z",
     "start_time": "2022-05-07T06:43:45.002081Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89  21  24  64\n",
       "1       Arry    C  36  37  37  57\n",
       "2        Ack    A  57  60  18  84\n",
       "3      Eorge    C  93  96  71  78\n",
       "4        Oah    D  65  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C  48  59  87  74\n",
       "96   Austin7    C  21  31  30  43\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "98       Eli    E  11  74  58  91\n",
       "99       Ben    E  21  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data = [1,2,3]\n",
    "# df = pd.DataFrame(data, dtype='float32') # 对所有字段指定类型\n",
    "# df\n",
    "\n",
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name='Sheet1', dtype={'team': 'string', 'Q1': 'int32'}) # 每个字段分别指定\n",
    "df\n",
    "# df.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08874534",
   "metadata": {},
   "source": [
    "### 自动推定类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "4a1a6232",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:47:21.826333Z",
     "start_time": "2022-05-07T06:47:21.771458Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (231699413.py, line 13)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [116]\u001b[1;36m\u001b[0m\n\u001b[1;33m    pd.to_numeric(m errors='coerce').fillna(0) # 兜底填充\u001b[0m\n\u001b[1;37m                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "df\n",
    "# 自动转换合适的数据类型\n",
    "df.convert_dtypes() # 推荐！新的方法，支持 string 类型\n",
    "df.infer_objects()\n",
    "\n",
    "# 按大体类型推定\n",
    "m = ['1', 2, 3]\n",
    "s = pd.to_numeric(m) # 转成数字\n",
    "pd.to_datetime(m) # 转成时间\n",
    "pd.to_timedelta(m) # 转成时差\n",
    "pd.to_datetime(m, errors='coerce') # 错误处理\n",
    "pd.to_numeric(m, errors='ignore')\n",
    "pd.to_numeric(m errors='coerce').fillna(0) # 兜底填充\n",
    "pd.to_datetime(df[['year', 'month', 'day']]) # 组合成日期\n",
    "\n",
    "# 最低期望\n",
    "pd.to_numeric(m, downcast='integer') # smallest signed int dtype\n",
    "# array([1, 2, 3], dtype=int8)\n",
    "pd.to_numeric(m, downcast='signed') # same as 'integer'\n",
    "# array([1, 2, 3], dtype=int8)\n",
    "pd.to_numeric(m, downcast='unsigned') # smallest unsigned int dtype\n",
    "# array([1, 2, 3], dtype=uint8)\n",
    "pd.to_numeric(m, downcast='float') # smallest float dtype\n",
    "# array([1., 2., 3.], dtype=float32)\n",
    "\n",
    "# 应用函数\n",
    "df.apply(pd.to_timedelta)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4c76a11",
   "metadata": {},
   "source": [
    "### astype() 类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "92d5ec13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89  21  24  64\n",
       "1       Arry    C  36  37  37  57\n",
       "2        Ack    A  57  60  18  84\n",
       "3      Eorge    C  93  96  71  78\n",
       "4        Oah    D  65  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C  48  59  87  74\n",
       "96   Austin7    C  21  31  30  43\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "98       Eli    E  11  74  58  91\n",
       "99       Ben    E  21  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1d9d6e99",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:50:51.640644Z",
     "start_time": "2022-05-07T06:50:51.603937Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89.0</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36.0</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57.0</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93.0</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65.0</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48.0</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21.0</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98.0</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11.0</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21.0</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team    Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89.0  21  24  64\n",
       "1       Arry    C  36.0  37  37  57\n",
       "2        Ack    A  57.0  60  18  84\n",
       "3      Eorge    C  93.0  96  71  78\n",
       "4        Oah    D  65.0  49  61  86\n",
       "..       ...  ...   ...  ..  ..  ..\n",
       "95   Gabriel    C  48.0  59  87  74\n",
       "96   Austin7    C  21.0  31  30  43\n",
       "97  Lincoln4    C  98.0  93   1  20\n",
       "98       Eli    E  11.0  74  58  91\n",
       "99       Ben    E  21.0  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.dtypes # 查看数据类型\n",
    "# df.index.astype('int64') # 索引类型转换\n",
    "# # df.astype('int32') # 所有数据转换为 int32\n",
    "df.astype({'Q1': 'float'}) # 指定字段转指定类型\n",
    "# s.astype('int64')\n",
    "# s.astype('int64', copy=False) # 不与原数据关联\n",
    "# s.astype(np.uint8)\n",
    "# df['name'].astype('object')\n",
    "# df['Q4'].astype('float')\n",
    "# s.astype('datetime64[ns]')\n",
    "# df['Q4'].astype('bool')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9635845",
   "metadata": {},
   "source": [
    "### 索引类型修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0146efb8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:54:26.097620Z",
     "start_time": "2022-05-07T06:54:26.075993Z"
    }
   },
   "outputs": [],
   "source": [
    "# df.rename() 可以用在链式方法中修改索引的数据类型\n",
    "\n",
    "df.rename(int)\n",
    "df.rename(str)\n",
    "df.rename(str, axis=1) # 修改行\n",
    "s.rename(int)\n",
    "# 其他方法\n",
    "df.index.astype(str)\n",
    "df.columns.astype(str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b81d05f",
   "metadata": {},
   "source": [
    "### 转换为时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87844900",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T06:58:23.939392Z",
     "start_time": "2022-05-07T06:58:23.930553Z"
    }
   },
   "outputs": [],
   "source": [
    "# pd.to_datetime() 和 s.astype('datetime64[ns]') 是最简单的时间转换方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b30b196b",
   "metadata": {},
   "source": [
    "## 数据的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be2fcce1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:05:38.965512Z",
     "start_time": "2022-05-07T07:05:38.954360Z"
    }
   },
   "outputs": [],
   "source": [
    "# 排序是数据分析的一种手段，Pandas支持三种排序方式：\n",
    "# 按索引标签排序\n",
    "# 按列值排序\n",
    "# 按索引和列值进行排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985164e2",
   "metadata": {},
   "source": [
    "### sort_index()索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "49aff97c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:28:39.806616Z",
     "start_time": "2022-05-07T07:28:39.760494Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "99       Ben    E  21  43  41  74\n",
       "98       Eli    E  11  74  58  91\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "96   Austin7    C  21  31  30  43\n",
       "95   Gabriel    C  48  59  87  74\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "4        Oah    D  65  49  61  86\n",
       "3      Eorge    C  93  96  71  78\n",
       "2        Ack    A  57  60  18  84\n",
       "1       Arry    C  36  37  37  57\n",
       "0      Liver    E  89  21  24  64\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sort_index() 可将索引重新排序，意味着每行数据的位置跟着索引而变化。\n",
    "\n",
    "\n",
    "# df.sort_index() # 按索引进行排序\n",
    "# df.team.sort_index() # team列进行升序排列\n",
    "df.sort_index(ascending=False) # 降序排列\n",
    "# df.sort_index(inplace=True) # 排序后生效，改变原数据\n",
    "# # 索引重新0-(n-1) 排, 很有用，可以得到它的排序号\n",
    "# s.sort_index(ignore_index=True)\n",
    "# s.sort_index(na_position='first') # 空值在前，另 ‘last’\n",
    "# s.sort_index(level=1) # 如果多层，排一级\n",
    "# s.sort_index(level=1, sort_remaining=False) # 这层不排\n",
    "\n",
    "# 行索引排序，表头排序\n",
    "# df.sort_index(axis=1) # 会把列按列名顺序排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "7329d928",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:32:10.406631Z",
     "start_time": "2022-05-07T07:32:10.368284Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   B  A\n",
       "a  3  1\n",
       "b  5  2\n",
       "c  6  4"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.reindex()指定自己定义顺序的索引，实现行和列的顺序重新定义：\n",
    "\n",
    "df = pd.DataFrame({\n",
    "    'A': [1,2,4],\n",
    "    'B': [3,5,6]\n",
    "}, index=['a', 'b', 'c']\n",
    ")\n",
    "df\n",
    "\n",
    "# 按要求重新指定索引顺序\n",
    "# df.reindex(['c','a','b'], axis = 0)\n",
    "\n",
    "# # 指定列顺序\n",
    "df.reindex(['B','A'],axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fbe72eb",
   "metadata": {},
   "source": [
    "### sort_values()数据值排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8a74a16",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:37:34.281088Z",
     "start_time": "2022-05-07T07:37:34.248483Z"
    }
   },
   "outputs": [],
   "source": [
    "# 数据值的排序主要使用sort_values(),数值按大小顺序，字符按字母顺序\n",
    "\n",
    "# series排序\n",
    "s.sort_values() # 升序\n",
    "s.sort_values(ascending=False) # 降序\n",
    "s.sort_values(inplace = True) # 修改生效\n",
    "s.sort_values(na_position='first') # 空值在前\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "73ea3636",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:40:34.333880Z",
     "start_time": "2022-05-07T07:40:34.240305Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>Nathan</td>\n",
       "      <td>A</td>\n",
       "      <td>87</td>\n",
       "      <td>77</td>\n",
       "      <td>62</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>Dylan</td>\n",
       "      <td>A</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>65</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>Blake</td>\n",
       "      <td>A</td>\n",
       "      <td>78</td>\n",
       "      <td>23</td>\n",
       "      <td>93</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>Jude</td>\n",
       "      <td>E</td>\n",
       "      <td>8</td>\n",
       "      <td>45</td>\n",
       "      <td>13</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>Rory9</td>\n",
       "      <td>E</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "      <td>58</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>Jackson5</td>\n",
       "      <td>E</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>Finn</td>\n",
       "      <td>E</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>55</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "88     Aaron    A  96  75  55   8\n",
       "17     Henry    A  91  15  75  17\n",
       "70    Nathan    A  87  77  62  13\n",
       "42     Dylan    A  86  87  65  20\n",
       "71     Blake    A  78  23  93   9\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "98       Eli    E  11  74  58  91\n",
       "43      Jude    E   8  45  13  65\n",
       "45     Rory9    E   8  12  58  27\n",
       "61  Jackson5    E   6  10  15  33\n",
       "82      Finn    E   4   1  55  32\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dataframe排序\n",
    "# df按指定字段排序\n",
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "df\n",
    "df.sort_values(by=['team'])  # 按team排序\n",
    "# df.sort_values('team')\n",
    "\n",
    "df.sort_values(by=['team','Q1']) # 先按team升序，再按Q1升序\n",
    "df.sort_values(by=['team','Q1'], ascending=False) # 降序\n",
    "df.sort_values(by=['team','Q1'], ascending=[True,False]) # team升序，Q1降序\n",
    "\n",
    "# # 索引重新0~（n-1）排序\n",
    "# df.sort_values(by=['team'],ignore_index=True)\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f2a3adc",
   "metadata": {},
   "source": [
    "### 索引和值同时排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c391882",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:57:50.299841Z",
     "start_time": "2022-05-07T07:57:50.259237Z"
    }
   },
   "outputs": [],
   "source": [
    "# 有的时候需要索引和值混合排序，比如先按名字排序，再按团队排序\n",
    "\n",
    "# 分步进行\n",
    "df.set_index('name', inplace=True)\n",
    "df.index.names = ['s_name']\n",
    "df.sort_values(by=['s_name','team'])\n",
    "\n",
    "# 综合\n",
    "df.set_index('name').sort_values('team').sort_index()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a6749f",
   "metadata": {},
   "source": [
    "### nsmallest() nlargest()获取TOPN条记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "5102e794",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T07:59:46.428372Z",
     "start_time": "2022-05-07T07:59:46.383481Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>Elijah</td>\n",
       "      <td>B</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "      <td>15</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Max</td>\n",
       "      <td>E</td>\n",
       "      <td>97</td>\n",
       "      <td>75</td>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>Aaron</td>\n",
       "      <td>A</td>\n",
       "      <td>96</td>\n",
       "      <td>75</td>\n",
       "      <td>55</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>Ryan</td>\n",
       "      <td>E</td>\n",
       "      <td>92</td>\n",
       "      <td>70</td>\n",
       "      <td>64</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>Alexander</td>\n",
       "      <td>C</td>\n",
       "      <td>91</td>\n",
       "      <td>76</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>Henry</td>\n",
       "      <td>A</td>\n",
       "      <td>91</td>\n",
       "      <td>15</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>Adam</td>\n",
       "      <td>C</td>\n",
       "      <td>90</td>\n",
       "      <td>32</td>\n",
       "      <td>47</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2  Q3  Q4\n",
       "97   Lincoln4    C  98  93   1  20\n",
       "38     Elijah    B  97  89  15  46\n",
       "19        Max    E  97  75  41   3\n",
       "88      Aaron    A  96  75  55   8\n",
       "3       Eorge    C  93  96  71  78\n",
       "80       Ryan    E  92  70  64  31\n",
       "32  Alexander    C  91  76  26  79\n",
       "17      Henry    A  91  15  75  17\n",
       "33       Adam    C  90  32  47  39\n",
       "0       Liver    E  89  21  24  64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用 nsmallest() 和 nlargest() 来实现排序（只支持数字）：\n",
    "\n",
    "# s.nsmallest(3) # 最小的三个\n",
    "# s.nlargest(3) # 最大的三个\n",
    "# 指定列\n",
    "# df.nlargest(10, 'Q1')\n",
    "df.nlargest(10, ['Q1', 'Q2']) \n",
    "# df.nsmallest(5, 'Q1')\n",
    "# df.nsmallest(5, ['Q1', 'Q2']) # Q1相同时， Q2进行排序，Q2小的在前"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6dd464a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:00:22.717886Z",
     "start_time": "2022-05-07T08:00:22.692126Z"
    }
   },
   "outputs": [],
   "source": [
    "# Series 还有一个 searchsorted 方法，用于假定在一个单调的序列中增加一个值，这个值所处的索引位置：\n",
    "\n",
    "ser.searchsorted(10)\n",
    "# 10\n",
    "ser.searchsorted([0, 4])\n",
    "# array([0, 3])\n",
    "ser.searchsorted([1, 3], side='right')\n",
    "# array([1, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "661ad256",
   "metadata": {},
   "source": [
    "## Pandas修改数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "880c5ea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89  21  24  64\n",
       "1       Arry    C  36  37  37  57\n",
       "2        Ack    A  57  60  18  84\n",
       "3      Eorge    C  93  96  71  78\n",
       "4        Oah    D  65  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C  48  59  87  74\n",
       "96   Austin7    C  21  31  30  43\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "98       Eli    E  11  74  58  91\n",
       "99       Ben    E  21  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "data = 'team.xlsx'\n",
    "\n",
    "df = pd.read_excel(data, sheet_name='Sheet1')\n",
    "df "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "d627e777",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2022-05-07T08:02:11.936Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>1</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>33</td>\n",
       "      <td>77</td>\n",
       "      <td>88</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>44</td>\n",
       "      <td>99</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>99</td>\n",
       "      <td>99</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>9</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>1</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>5</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>7</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>9</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E   1  21  24  64\n",
       "1       Arry    C  33  77  88  99\n",
       "2        Ack    A  44  99  18  84\n",
       "3      Eorge    C  99  99  71  78\n",
       "4        Oah    D   9  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C   1  59  87  74\n",
       "96   Austin7    C   3  31  30  43\n",
       "97  Lincoln4    C   5  93   1  20\n",
       "98       Eli    E   7  74  58  91\n",
       "99       Ben    E   9  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据分析过程中，对数据的修改调整也是比较多的，最简单的如学生成绩表加一列把总分算出来。\n",
    "# 根据某一列的数据解析出指定内容，如学生成绩单有个学生家庭地址，然后把所在小区解析出来形成新的一列。\n",
    "\n",
    "# Pandas 的数据修改是进行赋值，先把要修改的数据筛选出来，然后将同结构或者可解包的数据赋值给它：\n",
    "\n",
    "# df.Q1 = [1, 3, 5, 7, 9] * 20 # 就会把值进行修改\n",
    "# df.loc[1:3, 'Q1':'Q2'] = 99 # 这个范围的数据会全变成 99\n",
    "# df.loc[df.name=='Arry', 'Q1':'Q4'] = [66,77,88,99] # 指定多列\n",
    "df.loc[df.name.isin(['Arry', 'Ack']), 'Q1'] = (33, 44) # 修改列值\n",
    "df "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38dd4383",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:24:59.178625Z",
     "start_time": "2022-05-07T08:24:59.083167Z"
    }
   },
   "outputs": [],
   "source": [
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "df\n",
    "# df.columns  # 查看列头"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bde9fce",
   "metadata": {},
   "source": [
    "### rename()修改索引名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "d3a4d4e3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:18:18.828256Z",
     "start_time": "2022-05-07T08:18:18.790011Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "a  1  3\n",
       "b  2  5\n",
       "c  4  6"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "\n",
    "df = pd.DataFrame({\n",
    "    'A': [1,2,4],\n",
    "    'B': [3,5,6]\n",
    "}, index=['a', 'b', 'c']\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "f9d4e63c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:18:22.835902Z",
     "start_time": "2022-05-07T08:18:22.688244Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "a  1  3\n",
       "2  2  5\n",
       "3  4  6"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对索引的修改包含行索引和列索引：\n",
    "\n",
    "# 对索引值进行修改\n",
    "# df.rename(columns={\"Q1\": \"a\", \"Q2\": \"b\"}) # 对表头进行修改\n",
    "# df.rename(index={'a': \"x\", 'b': \"y\", 'c': \"z\"}) # 对索引进行修改\n",
    "\n",
    "# df.rename(index=str) # 对类型进行修改\n",
    "# df.rename(str.lower, axis='columns') # 列索引转小写\n",
    "df.rename({'b': 2, 'c': 3}, axis='index')\n",
    "\n",
    "# # 对索引名进行修改\n",
    "# s.rename_axis(\"animal\")\n",
    "# df.rename_axis(\"序号\"，axis='rows') # 默认是列索引,axis参数可以不写\n",
    "# df.rename_axis(\"种类\", axis=\"columns\") # 指定行索引\n",
    "# # 多层索引时可以将type修改为class\n",
    "# df.rename_axis(index={'type': 'class'})\n",
    "\n",
    "# # 可以用 set_axis 进行设置修改\n",
    "# s.set_axis(['a', 'b', 'c'], axis=0)\n",
    "# df.set_axis(['I', 'II'], axis='columns')\n",
    "# df.set_axis(['i', 'ii'], axis='columns', inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd96193f",
   "metadata": {},
   "source": [
    "### replace()修改指定的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "da212079",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:19:23.315519Z",
     "start_time": "2022-05-07T08:19:23.290013Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "a  4  4\n",
       "b  4  5\n",
       "c  4  6"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 替换数据：\n",
    "df\n",
    "df.replace(5, 0) # 将数据中所有 5 换为 0\n",
    "df.replace([0, 1, 2, 3], 4) # 将 0-3 全换成 4\n",
    "df.replace([0, 1, 2, 3], [4, 3, 2, 1]) # 对应修改\n",
    "# # {‘pad’, ‘ffill’, ‘bfill’, None} 试试\n",
    "# s.replace([1, 2], method='bfill') # 向下填充\n",
    "# df.replace({0: 10, 1: 100}) # 字典对应修改\n",
    "df.replace({'Q1': 0, 'Q2': 5}, 100) # 指定字段的指定值修改为 100\n",
    "df.replace({'Q1': {0: 100, 4: 400}}) # 指定列里指定值按指定的值替换\n",
    "# # 使用正则\n",
    "# df.replace(to_replace=r'^ba.$', value='new', regex=True)\n",
    "# df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)\n",
    "# df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'})\n",
    "# df.replace(regex=[r'^ba.$', 'foo'], value='new')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7003f91c",
   "metadata": {},
   "source": [
    "### dropna空值删除填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "557bcb56",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:22:39.884751Z",
     "start_time": "2022-05-07T08:22:39.861632Z"
    }
   },
   "outputs": [],
   "source": [
    "# 删除空值：\n",
    "\n",
    "df.dropna() # 一行中有一个空NaN就删除\n",
    "df.dropna(axis='columns') # 只保留全有值的列\n",
    "df.dropna(how='all') # 行或列全没值才删除\n",
    "df.dropna(thresh=2) # 至少有两个空值时才删除\n",
    "df.dropna(inplace=True) # 删除并生效替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c712e0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 填充空值：\n",
    "\n",
    "df.fillna(0) # 空全修改为 0\n",
    "# {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None\n",
    "df.fillna(method='ffill') # 都修改为它前一个值\n",
    "values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}\n",
    "df.fillna(value=values) # 各列替换空值不同\n",
    "df.fillna(value=values, limit=1) # 只替换第一个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4534cff2",
   "metadata": {},
   "source": [
    "### 添加行列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17f0743f",
   "metadata": {},
   "source": [
    "#### 添加列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "31af1925",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "35249cb1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:31:49.292800Z",
     "start_time": "2022-05-07T08:31:49.148827Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>foo</th>\n",
       "      <th>成绩</th>\n",
       "      <th>Q1成绩</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "      <td>110</td>\n",
       "      <td>合格</td>\n",
       "      <td>合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "      <td>73</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>117</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "      <td>189</td>\n",
       "      <td>合格</td>\n",
       "      <td>合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "      <td>114</td>\n",
       "      <td>合格</td>\n",
       "      <td>合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "      <td>107</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "      <td>52</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>191</td>\n",
       "      <td>合格</td>\n",
       "      <td>合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "      <td>85</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "      <td>64</td>\n",
       "      <td>不合格</td>\n",
       "      <td>不合格</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4  foo   成绩 Q1成绩\n",
       "0      Liver    E  89  21  24  64  110   合格   合格\n",
       "1       Arry    C  36  37  37  57   73  不合格  不合格\n",
       "2        Ack    A  57  60  18  84  117  不合格  不合格\n",
       "3      Eorge    C  93  96  71  78  189   合格   合格\n",
       "4        Oah    D  65  49  61  86  114   合格   合格\n",
       "..       ...  ...  ..  ..  ..  ..  ...  ...  ...\n",
       "95   Gabriel    C  48  59  87  74  107  不合格  不合格\n",
       "96   Austin7    C  21  31  30  43   52  不合格  不合格\n",
       "97  Lincoln4    C  98  93   1  20  191   合格   合格\n",
       "98       Eli    E  11  74  58  91   85  不合格  不合格\n",
       "99       Ben    E  21  43  41  74   64  不合格  不合格\n",
       "\n",
       "[100 rows x 9 columns]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Pandas 的数据添加修改非常简单，新赋值一个原 df 中没有列名就会产生一个新列：\n",
    "\n",
    "# df['foo'] = 100 # 增加一列 foo, 所有值都是 100\n",
    "df['foo'] = df.Q1 + df.Q2 # 新列为两列相加\n",
    "# df['foo'] = df['Q1'] + df['Q2'] # 同上\n",
    "\n",
    "# # 把所有为数字的加起来\n",
    "# df['total'] = df.select_dtypes(include=['int']).sum(1)\n",
    "# df['total'] = df.loc[:,'Q1':'Q4'].apply(lambda x: sum(x), axis='columns')\n",
    "# df.loc[:, 'Q10'] = '我是新来的' # 也可以\n",
    "\n",
    "# # 增加一列并赋值，不满足条件的为 NaN\n",
    "df.loc[df.Q1 >= 60, 'Q1成绩'] = '合格'\n",
    "df.loc[df.Q1 < 60, 'Q1成绩'] = '不合格'\n",
    "\n",
    "df "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6fb8582",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用df.assign()指定新列：\n",
    "\n",
    "df.assign(Q5=[100]*100) # 新增加一列 Q5\n",
    "df = df.assign(Q5=[100]*100) # 赋值生效\n",
    "df.assign(Q6=df.Q2/df.Q1) # 计算并增加 Q6\n",
    "df.assign(Q7=lambda x: x.Q1 * 9 / 5 + 32) # 使用 lambda\n",
    "# 添加一列，值为表达式结果 True or False\n",
    "df.assign(tag=df.Q1>df.Q2)\n",
    "# True 为1 False 为 0\n",
    "df.assign(tag=(df.Q1>df.Q2).astype(int))\n",
    "# 映射文案\n",
    "df.assign(tag=(df.Q1>60).map({True:'及格',False:'不及格'}))\n",
    "# 增加多个\n",
    "df.assign(Q8=lambda x: x.Q1*5,\n",
    "         Q9=lambda x: x.Q8+1) # 注 Q8没生效不能直接 df.Q8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f5de887",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 df.insert() 插入新的行，会立即生效：\n",
    "\n",
    "# 一般格式 df.insert(新列索引位, 名字, 数据)\n",
    "df.insert(len(df.columns), 'Qx',\n",
    "          pd.Series(np.random.randn(100), index=df.index))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839b97c5",
   "metadata": {},
   "source": [
    "#### 添加行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "5cfe548d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "a0bc4f41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24.0</td>\n",
       "      <td>64.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37.0</td>\n",
       "      <td>57.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18.0</td>\n",
       "      <td>84.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71.0</td>\n",
       "      <td>78.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61.0</td>\n",
       "      <td>86.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58.0</td>\n",
       "      <td>91.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41.0</td>\n",
       "      <td>74.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>tom</td>\n",
       "      <td>A</td>\n",
       "      <td>88</td>\n",
       "      <td>88</td>\n",
       "      <td>88.0</td>\n",
       "      <td>88.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>88</td>\n",
       "      <td>99</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>102 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         name team  Q1  Q2    Q3    Q4\n",
       "0       Liver    E  89  21  24.0  64.0\n",
       "1        Arry    C  36  37  37.0  57.0\n",
       "2         Ack    A  57  60  18.0  84.0\n",
       "3       Eorge    C  93  96  71.0  78.0\n",
       "4         Oah    D  65  49  61.0  86.0\n",
       "..        ...  ...  ..  ..   ...   ...\n",
       "97   Lincoln4    C  98  93   1.0  20.0\n",
       "98        Eli    E  11  74  58.0  91.0\n",
       "99        Ben    E  21  43  41.0  74.0\n",
       "101       tom    A  88  88  88.0  88.0\n",
       "102       NaN  NaN  88  99   NaN   NaN\n",
       "\n",
       "[102 rows x 6 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最简单的办法利用新索引，按位置给出新数据的列表：\n",
    "\n",
    "df.loc[101] = ['tom', 'A', 88, 88, 88, 88]\n",
    "df.loc[102]={'Q1':88,'Q2':99} # 指定列，无数据列值为NaN\n",
    "df.loc[df.shape[0]+1] = {'Q1':88,'Q2':99} # df.shape[0] 计算出索引最大值，自动增加索引\n",
    "# df.loc[len(df)+1] = {'Q1':88,'Q2':99}\n",
    "df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d60b08e6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:47:34.750765Z",
     "start_time": "2022-05-07T08:47:34.595752Z"
    }
   },
   "outputs": [],
   "source": [
    "# 如果需要批量操作，可以使用迭代的办法\n",
    "df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))\n",
    "rows = [[1,2],[3,4],[5,6]]\n",
    "for row in rows:\n",
    "    df.loc[len(df)] = row\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7038a75c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-07T08:47:42.277525Z",
     "start_time": "2022-05-07T08:47:42.242434Z"
    }
   },
   "outputs": [],
   "source": [
    "# df.append() 可以追加一个新行\n",
    "df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))\n",
    "df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))\n",
    "df.append(df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd86bd91",
   "metadata": {},
   "source": [
    "#### 连接两个df/s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c94f9b5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:05:53.731807Z",
     "start_time": "2022-05-09T01:05:53.685517Z"
    }
   },
   "outputs": [],
   "source": [
    "# pd.concat([s1,s2]) 将两个dataframe/series连接起来\n",
    "# series\n",
    "s1 = pd.Series(['a','b'])\n",
    "s2 = pd.Series(['c','d'])\n",
    "pd.concat([s1,s2])\n",
    "pd.concat([s1,s2], ignore_index=True) # 忽略索引，重新编排索引\n",
    "\n",
    "pd.concat([s1,s2],keys=['s1','s2'])  # 原数据索引不变，增加一个一层索引（keys里的内容），变成多层索引\n",
    "pd.concat([s1, s2], keys=['s1', 's2'],\n",
    "          names=['Series name', 'Row ID']) # 给索引命名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "400746cf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:09:10.285246Z",
     "start_time": "2022-05-09T01:09:10.249327Z"
    }
   },
   "outputs": [],
   "source": [
    "# dataframe\n",
    "pd.concat([df1,df2])\n",
    "pd.concat([df1,df2], sort=True) # 结果排序\n",
    "pd.concat([df1, df3], join=\"inner\") # 只连相同列\n",
    "pd.concat([df1, df4], axis=1) # 连接列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e6d0acd",
   "metadata": {},
   "source": [
    "#### 删除行列 pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "d1bb6968",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:19:19.846273Z",
     "start_time": "2022-05-09T01:19:19.749007Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "3",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3621\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   3620\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 3621\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\_libs\\index.pyx:136\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\_libs\\index.pyx:163\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi:5198\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "File \u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi:5206\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 3",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [101]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# pop()\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# df.pop('Q1') # 删除Q1列\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:5270\u001b[0m, in \u001b[0;36mDataFrame.pop\u001b[1;34m(self, item)\u001b[0m\n\u001b[0;32m   5229\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpop\u001b[39m(\u001b[38;5;28mself\u001b[39m, item: Hashable) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Series:\n\u001b[0;32m   5230\u001b[0m     \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m   5231\u001b[0m \u001b[38;5;124;03m    Return item and drop from frame. Raise KeyError if not found.\u001b[39;00m\n\u001b[0;32m   5232\u001b[0m \n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   5268\u001b[0m \u001b[38;5;124;03m    3  monkey        NaN\u001b[39;00m\n\u001b[0;32m   5269\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 5270\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mitem\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mitem\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py:865\u001b[0m, in \u001b[0;36mNDFrame.pop\u001b[1;34m(self, item)\u001b[0m\n\u001b[0;32m    864\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpop\u001b[39m(\u001b[38;5;28mself\u001b[39m, item: Hashable) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Series \u001b[38;5;241m|\u001b[39m Any:\n\u001b[1;32m--> 865\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mitem\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m    866\u001b[0m     \u001b[38;5;28;01mdel\u001b[39;00m \u001b[38;5;28mself\u001b[39m[item]\n\u001b[0;32m    868\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:3505\u001b[0m, in \u001b[0;36mDataFrame.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   3503\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mnlevels \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m   3504\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_multilevel(key)\n\u001b[1;32m-> 3505\u001b[0m indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumns\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   3506\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[0;32m   3507\u001b[0m     indexer \u001b[38;5;241m=\u001b[39m [indexer]\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3623\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m   3621\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine\u001b[38;5;241m.\u001b[39mget_loc(casted_key)\n\u001b[0;32m   3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[1;32m-> 3623\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m   3624\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m   3625\u001b[0m     \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[0;32m   3626\u001b[0m     \u001b[38;5;66;03m#  InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[0;32m   3627\u001b[0m     \u001b[38;5;66;03m#  the TypeError.\u001b[39;00m\n\u001b[0;32m   3628\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n",
      "\u001b[1;31mKeyError\u001b[0m: 3"
     ]
    }
   ],
   "source": [
    "# pop()\n",
    "# df.pop('Q1') # 删除Q1列\n",
    "s.pop(3)  # series删除索引为3的行\n",
    "# 也可以把想要的列筛选出来，赋值给新的df，达到删除的目的\n",
    "\n",
    "# df "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "101e18cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:31:39.462107Z",
     "start_time": "2022-05-09T01:31:39.447081Z"
    }
   },
   "source": [
    "#### df.update() 进行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41b65dd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:48:07.802262Z",
     "start_time": "2022-05-09T01:48:07.787377Z"
    }
   },
   "outputs": [],
   "source": [
    "# 创建df\n",
    "df = pd.DataFrame({'A': [1, 2, 3],\n",
    "                   'B': [4, 5, 6]},\n",
    "                  index=['x', 'y', 'z']\n",
    "                 )\n",
    "# 修改(y,B) 的值为99\n",
    "df.update(pd.Series([99],index=['y'], name='B'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39e692af",
   "metadata": {},
   "source": [
    "### drop()删除数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "561eaac8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T01:57:41.899098Z",
     "start_time": "2022-05-09T01:57:41.861754Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B   C   D\n",
       "0  0  1   2   3\n",
       "1  4  5   6   7\n",
       "2  8  9  10  11"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd \n",
    "# df.drop()语法\n",
    "'''\n",
    "df.drop(labels = None, axis = 0,\n",
    "        index= None, columns=None,\n",
    "        level= None, inplace=False,\n",
    "        errors='raise'\n",
    "       )\n",
    "'''\n",
    "# labels：要删除的列或者行，多个传入列表\n",
    "# axis：轴的方向，0为行，1为列，默认为0\n",
    "# index：指定的一个行或者多个行，\n",
    "# column：指定的一个列或者多个列\n",
    "# level：索引层级，将删除此层级\n",
    "# inplace：布尔值，是否生效\n",
    "# errors：ignore或者raise，默认raise，如果为ignore，则抑制错误并仅删除现有标签\n",
    "\n",
    "df = pd.DataFrame(np.arange(12).reshape(3,4),\n",
    "                 columns=['A','B','C','D'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cada3c9",
   "metadata": {},
   "source": [
    "#### 删除列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "785b2880",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2022-05-09T02:01:01.640Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   D\n",
       "0  0   3\n",
       "1  4   7\n",
       "2  8  11"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(['B','C'],axis=1) # 删除B C列\n",
    "df.drop(columns=['B','C']) # 同上\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcf1502f",
   "metadata": {},
   "source": [
    "#### 删除行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "884893be",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:03:43.580279Z",
     "start_time": "2022-05-09T02:03:43.554014Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B   C   D\n",
       "2  8  9  10  11"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop([0])   # 删除索引0\n",
    "df.drop([0,1])  # 删除索引0，1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c620743b",
   "metadata": {},
   "source": [
    "#### 多层索引删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "31694a0c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:06:12.119131Z",
     "start_time": "2022-05-09T02:06:12.077373Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">A</th>\n",
       "      <th>speed</th>\n",
       "      <td>45.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>200.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">B</th>\n",
       "      <th>speed</th>\n",
       "      <td>30.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>250.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>1.5</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">C</th>\n",
       "      <th>speed</th>\n",
       "      <td>320.0</td>\n",
       "      <td>250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>weight</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>0.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            big  small\n",
       "A speed    45.0   30.0\n",
       "  weight  200.0  100.0\n",
       "  length    1.5    1.0\n",
       "B speed    30.0   20.0\n",
       "  weight  250.0  150.0\n",
       "  length    1.5    0.8\n",
       "C speed   320.0  250.0\n",
       "  weight    1.0    0.8\n",
       "  length    0.3    0.2"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "midx = pd.MultiIndex(levels=[['A', 'B', 'C'],\n",
    "                             ['speed', 'weight', 'length']],\n",
    "                     codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],\n",
    "                            [0, 1, 2, 0, 1, 2, 0, 1, 2]])\n",
    "\n",
    "# midx\n",
    "\n",
    "df = pd.DataFrame(index=midx, columns=['big', 'small'],\n",
    "                  data=[[45, 30], [200, 100], [1.5, 1], [30, 20],\n",
    "                        [250, 150], [1.5, 0.8], [320, 250],\n",
    "                        [1, 0.8], [0.3, 0.2]])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "2ef98f2d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:13:45.412057Z",
     "start_time": "2022-05-09T02:13:45.355292Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>weight</th>\n",
       "      <td>200.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>weight</th>\n",
       "      <td>250.0</td>\n",
       "      <td>150.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>1.5</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C</th>\n",
       "      <th>weight</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>length</th>\n",
       "      <td>0.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            big  small\n",
       "A weight  200.0  100.0\n",
       "  length    1.5    1.0\n",
       "B weight  250.0  150.0\n",
       "  length    1.5    0.8\n",
       "C weight    1.0    0.8\n",
       "  length    0.3    0.2"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除行\n",
    "# df.drop(index='A')\n",
    "# 删除列\n",
    "# df.drop(columns='small')\n",
    "# # 删除行和列\n",
    "# df.drop(index='A', columns='small')\n",
    "\n",
    "# # # 指定层级进行删除\n",
    "df.drop(index='C', level=0)\n",
    "\n",
    "df.drop(index='speed', level=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fc7a442",
   "metadata": {},
   "source": [
    "## Pandas 的数据遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "dda26f76",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:41:21.753100Z",
     "start_time": "2022-05-09T02:41:21.670616Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['name', 'team', 'Q1', 'Q2', 'Q3', 'Q4'], dtype='object')"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建数据\n",
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "\n",
    "df.columns  # 查看列头"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8956d0d",
   "metadata": {},
   "source": [
    "### DF按行遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ad670b6d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:25:43.487616Z",
     "start_time": "2022-05-09T02:25:43.387392Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 Liver E\n",
      "1 Arry C\n",
      "2 Ack A\n",
      "3 Eorge C\n",
      "4 Oah D\n",
      "5 Harlie C\n",
      "6 Acob B\n",
      "7 Lfie A\n",
      "8 Reddie D\n",
      "9 Oscar A\n",
      "10 Leo B\n",
      "11 Logan B\n",
      "12 Archie C\n",
      "13 Theo C\n",
      "14 Thomas B\n",
      "15 James E\n",
      "16 Joshua A\n",
      "17 Henry A\n",
      "18 William C\n",
      "19 Max E\n",
      "20 Lucas A\n",
      "21 Ethan D\n",
      "22 Arthur A\n",
      "23 Mason D\n",
      "24 Isaac E\n",
      "25 Harrison B\n",
      "26 Teddy E\n",
      "27 Finley D\n",
      "28 Daniel C\n",
      "29 Riley E\n",
      "30 Edward B\n",
      "31 Joseph E\n",
      "32 Alexander C\n",
      "33 Adam C\n",
      "34 Reggie1 A\n",
      "35 Samuel B\n",
      "36 Jaxon E\n",
      "37 Sebastian C\n",
      "38 Elijah B\n",
      "39 Harley B\n",
      "40 Toby A\n",
      "41 Arlo8 E\n",
      "42 Dylan A\n",
      "43 Jude E\n",
      "44 Benjamin D\n",
      "45 Rory9 E\n",
      "46 Tommy C\n",
      "47 Jake3 C\n",
      "48 Louie D\n",
      "49 Carter7 D\n",
      "50 Jenson B\n",
      "51 Hugo0 A\n",
      "52 Bobby1 D\n",
      "53 Frankie B\n",
      "54 Ollie3 C\n",
      "55 Zachary E\n",
      "56 David B\n",
      "57 Albie1 D\n",
      "58 Lewis B\n",
      "59 Luca D\n",
      "60 Ronnie B\n",
      "61 Jackson5 E\n",
      "62 Matthew C\n",
      "63 Alex D\n",
      "64 Harvey2 B\n",
      "65 Reuben D\n",
      "66 Jayden6 D\n",
      "67 Caleb A\n",
      "68 Hunter3 D\n",
      "69 Theodore3 D\n",
      "70 Nathan A\n",
      "71 Blake A\n",
      "72 Luke6 D\n",
      "73 Elliot C\n",
      "74 Roman E\n",
      "75 Stanley A\n",
      "76 Dexter E\n",
      "77 Michael B\n",
      "78 Elliott B\n",
      "79 Tyler A\n",
      "80 Ryan E\n",
      "81 Ellis C\n",
      "82 Finn E\n",
      "83 Albert0 B\n",
      "84 Kai B\n",
      "85 Liam B\n",
      "86 Calum C\n",
      "87 Louis2 C\n",
      "88 Aaron A\n",
      "89 Ezra D\n",
      "90 Leon E\n",
      "91 Connor C\n",
      "92 Grayson7 B\n",
      "93 Jamie0 B\n",
      "94 Aiden D\n",
      "95 Gabriel C\n",
      "96 Austin7 C\n",
      "97 Lincoln4 C\n",
      "98 Eli E\n",
      "99 Ben E\n",
      "0 0 E\n",
      "1 1 C\n",
      "2 2 A\n",
      "3 3 C\n",
      "4 4 D\n",
      "5 5 C\n",
      "6 6 B\n",
      "7 7 A\n",
      "8 8 D\n",
      "9 9 A\n",
      "10 10 B\n",
      "11 11 B\n",
      "12 12 C\n",
      "13 13 C\n",
      "14 14 B\n",
      "15 15 E\n",
      "16 16 A\n",
      "17 17 A\n",
      "18 18 C\n",
      "19 19 E\n",
      "20 20 A\n",
      "21 21 D\n",
      "22 22 A\n",
      "23 23 D\n",
      "24 24 E\n",
      "25 25 B\n",
      "26 26 E\n",
      "27 27 D\n",
      "28 28 C\n",
      "29 29 E\n",
      "30 30 B\n",
      "31 31 E\n",
      "32 32 C\n",
      "33 33 C\n",
      "34 34 A\n",
      "35 35 B\n",
      "36 36 E\n",
      "37 37 C\n",
      "38 38 B\n",
      "39 39 B\n",
      "40 40 A\n",
      "41 41 E\n",
      "42 42 A\n",
      "43 43 E\n",
      "44 44 D\n",
      "45 45 E\n",
      "46 46 C\n",
      "47 47 C\n",
      "48 48 D\n",
      "49 49 D\n",
      "50 50 B\n",
      "51 51 A\n",
      "52 52 D\n",
      "53 53 B\n",
      "54 54 C\n",
      "55 55 E\n",
      "56 56 B\n",
      "57 57 D\n",
      "58 58 B\n",
      "59 59 D\n",
      "60 60 B\n",
      "61 61 E\n",
      "62 62 C\n",
      "63 63 D\n",
      "64 64 B\n",
      "65 65 D\n",
      "66 66 D\n",
      "67 67 A\n",
      "68 68 D\n",
      "69 69 D\n",
      "70 70 A\n",
      "71 71 A\n",
      "72 72 D\n",
      "73 73 C\n",
      "74 74 E\n",
      "75 75 A\n",
      "76 76 E\n",
      "77 77 B\n",
      "78 78 B\n",
      "79 79 A\n",
      "80 80 E\n",
      "81 81 C\n",
      "82 82 E\n",
      "83 83 B\n",
      "84 84 B\n",
      "85 85 B\n",
      "86 86 C\n",
      "87 87 C\n",
      "88 88 A\n",
      "89 89 D\n",
      "90 90 E\n",
      "91 91 C\n",
      "92 92 B\n",
      "93 93 B\n",
      "94 94 D\n",
      "95 95 C\n",
      "96 96 C\n",
      "97 97 C\n",
      "98 98 E\n",
      "99 99 E\n"
     ]
    }
   ],
   "source": [
    "\n",
    "for index,row in df.iterrows():\n",
    "    print(index, row['name'], row['team'])\n",
    "    \n",
    "for index,row in df.iterrows():\n",
    "    print(index, row.name, row.team)    \n",
    "# 注意：  row.name 和 row['name']是不一样的； row.team 和 row['team']是一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "64d77baa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:37:38.872797Z",
     "start_time": "2022-05-09T02:37:38.834711Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pandas(Index=0, name='Liver', team='E', Q1=89, Q2=21, Q3=24, Q4=64)\n",
      "Pandas(Index=1, name='Arry', team='C', Q1=36, Q2=37, Q3=37, Q4=57)\n",
      "Pandas(Index=2, name='Ack', team='A', Q1=57, Q2=60, Q3=18, Q4=84)\n",
      "Pandas(Index=3, name='Eorge', team='C', Q1=93, Q2=96, Q3=71, Q4=78)\n",
      "Pandas(Index=4, name='Oah', team='D', Q1=65, Q2=49, Q3=61, Q4=86)\n",
      "Pandas(Index=5, name='Harlie', team='C', Q1=24, Q2=13, Q3=87, Q4=43)\n",
      "Pandas(Index=6, name='Acob', team='B', Q1=61, Q2=95, Q3=94, Q4=8)\n",
      "Pandas(Index=7, name='Lfie', team='A', Q1=9, Q2=10, Q3=99, Q4=37)\n",
      "Pandas(Index=8, name='Reddie', team='D', Q1=64, Q2=93, Q3=57, Q4=72)\n",
      "Pandas(Index=9, name='Oscar', team='A', Q1=77, Q2=9, Q3=26, Q4=67)\n",
      "Pandas(Index=10, name='Leo', team='B', Q1=17, Q2=4, Q3=33, Q4=79)\n",
      "Pandas(Index=11, name='Logan', team='B', Q1=9, Q2=89, Q3=35, Q4=65)\n",
      "Pandas(Index=12, name='Archie', team='C', Q1=83, Q2=89, Q3=59, Q4=68)\n",
      "Pandas(Index=13, name='Theo', team='C', Q1=51, Q2=86, Q3=87, Q4=27)\n",
      "Pandas(Index=14, name='Thomas', team='B', Q1=80, Q2=48, Q3=56, Q4=41)\n",
      "Pandas(Index=15, name='James', team='E', Q1=48, Q2=77, Q3=52, Q4=11)\n",
      "Pandas(Index=16, name='Joshua', team='A', Q1=63, Q2=4, Q3=80, Q4=30)\n",
      "Pandas(Index=17, name='Henry', team='A', Q1=91, Q2=15, Q3=75, Q4=17)\n",
      "Pandas(Index=18, name='William', team='C', Q1=80, Q2=68, Q3=3, Q4=26)\n",
      "Pandas(Index=19, name='Max', team='E', Q1=97, Q2=75, Q3=41, Q4=3)\n",
      "Pandas(Index=20, name='Lucas', team='A', Q1=60, Q2=41, Q3=77, Q4=62)\n",
      "Pandas(Index=21, name='Ethan', team='D', Q1=79, Q2=45, Q3=89, Q4=88)\n",
      "Pandas(Index=22, name='Arthur', team='A', Q1=44, Q2=53, Q3=42, Q4=40)\n",
      "Pandas(Index=23, name='Mason', team='D', Q1=80, Q2=96, Q3=26, Q4=49)\n",
      "Pandas(Index=24, name='Isaac', team='E', Q1=74, Q2=23, Q3=28, Q4=65)\n",
      "Pandas(Index=25, name='Harrison', team='B', Q1=89, Q2=13, Q3=18, Q4=75)\n",
      "Pandas(Index=26, name='Teddy', team='E', Q1=71, Q2=91, Q3=21, Q4=48)\n",
      "Pandas(Index=27, name='Finley', team='D', Q1=62, Q2=73, Q3=84, Q4=68)\n",
      "Pandas(Index=28, name='Daniel', team='C', Q1=50, Q2=50, Q3=72, Q4=61)\n",
      "Pandas(Index=29, name='Riley', team='E', Q1=35, Q2=26, Q3=59, Q4=83)\n",
      "Pandas(Index=30, name='Edward', team='B', Q1=57, Q2=38, Q3=86, Q4=87)\n",
      "Pandas(Index=31, name='Joseph', team='E', Q1=67, Q2=87, Q3=87, Q4=93)\n",
      "Pandas(Index=32, name='Alexander', team='C', Q1=91, Q2=76, Q3=26, Q4=79)\n",
      "Pandas(Index=33, name='Adam', team='C', Q1=90, Q2=32, Q3=47, Q4=39)\n",
      "Pandas(Index=34, name='Reggie1', team='A', Q1=30, Q2=12, Q3=23, Q4=9)\n",
      "Pandas(Index=35, name='Samuel', team='B', Q1=9, Q2=38, Q3=88, Q4=66)\n",
      "Pandas(Index=36, name='Jaxon', team='E', Q1=88, Q2=98, Q3=19, Q4=98)\n",
      "Pandas(Index=37, name='Sebastian', team='C', Q1=1, Q2=14, Q3=68, Q4=48)\n",
      "Pandas(Index=38, name='Elijah', team='B', Q1=97, Q2=89, Q3=15, Q4=46)\n",
      "Pandas(Index=39, name='Harley', team='B', Q1=2, Q2=99, Q3=12, Q4=13)\n",
      "Pandas(Index=40, name='Toby', team='A', Q1=52, Q2=27, Q3=17, Q4=68)\n",
      "Pandas(Index=41, name='Arlo8', team='E', Q1=48, Q2=34, Q3=52, Q4=51)\n",
      "Pandas(Index=42, name='Dylan', team='A', Q1=86, Q2=87, Q3=65, Q4=20)\n",
      "Pandas(Index=43, name='Jude', team='E', Q1=8, Q2=45, Q3=13, Q4=65)\n",
      "Pandas(Index=44, name='Benjamin', team='D', Q1=15, Q2=88, Q3=52, Q4=25)\n",
      "Pandas(Index=45, name='Rory9', team='E', Q1=8, Q2=12, Q3=58, Q4=27)\n",
      "Pandas(Index=46, name='Tommy', team='C', Q1=29, Q2=44, Q3=28, Q4=76)\n",
      "Pandas(Index=47, name='Jake3', team='C', Q1=69, Q2=23, Q3=11, Q4=40)\n",
      "Pandas(Index=48, name='Louie', team='D', Q1=24, Q2=84, Q3=54, Q4=11)\n",
      "Pandas(Index=49, name='Carter7', team='D', Q1=57, Q2=52, Q3=77, Q4=50)\n",
      "Pandas(Index=50, name='Jenson', team='B', Q1=66, Q2=77, Q3=88, Q4=74)\n",
      "Pandas(Index=51, name='Hugo0', team='A', Q1=28, Q2=25, Q3=14, Q4=71)\n",
      "Pandas(Index=52, name='Bobby1', team='D', Q1=50, Q2=55, Q3=60, Q4=59)\n",
      "Pandas(Index=53, name='Frankie', team='B', Q1=18, Q2=62, Q3=52, Q4=33)\n",
      "Pandas(Index=54, name='Ollie3', team='C', Q1=10, Q2=76, Q3=30, Q4=36)\n",
      "Pandas(Index=55, name='Zachary', team='E', Q1=12, Q2=71, Q3=85, Q4=93)\n",
      "Pandas(Index=56, name='David', team='B', Q1=21, Q2=47, Q3=99, Q4=2)\n",
      "Pandas(Index=57, name='Albie1', team='D', Q1=79, Q2=82, Q3=56, Q4=96)\n",
      "Pandas(Index=58, name='Lewis', team='B', Q1=4, Q2=34, Q3=77, Q4=28)\n",
      "Pandas(Index=59, name='Luca', team='D', Q1=5, Q2=40, Q3=91, Q4=83)\n",
      "Pandas(Index=60, name='Ronnie', team='B', Q1=53, Q2=13, Q3=34, Q4=99)\n",
      "Pandas(Index=61, name='Jackson5', team='E', Q1=6, Q2=10, Q3=15, Q4=33)\n",
      "Pandas(Index=62, name='Matthew', team='C', Q1=44, Q2=33, Q3=41, Q4=98)\n",
      "Pandas(Index=63, name='Alex', team='D', Q1=14, Q2=70, Q3=55, Q4=87)\n",
      "Pandas(Index=64, name='Harvey2', team='B', Q1=43, Q2=76, Q3=87, Q4=90)\n",
      "Pandas(Index=65, name='Reuben', team='D', Q1=70, Q2=72, Q3=76, Q4=56)\n",
      "Pandas(Index=66, name='Jayden6', team='D', Q1=64, Q2=21, Q3=10, Q4=21)\n",
      "Pandas(Index=67, name='Caleb', team='A', Q1=64, Q2=34, Q3=46, Q4=88)\n",
      "Pandas(Index=68, name='Hunter3', team='D', Q1=38, Q2=80, Q3=82, Q4=40)\n",
      "Pandas(Index=69, name='Theodore3', team='D', Q1=43, Q2=7, Q3=68, Q4=80)\n",
      "Pandas(Index=70, name='Nathan', team='A', Q1=87, Q2=77, Q3=62, Q4=13)\n",
      "Pandas(Index=71, name='Blake', team='A', Q1=78, Q2=23, Q3=93, Q4=9)\n",
      "Pandas(Index=72, name='Luke6', team='D', Q1=15, Q2=97, Q3=95, Q4=99)\n",
      "Pandas(Index=73, name='Elliot', team='C', Q1=15, Q2=17, Q3=76, Q4=22)\n",
      "Pandas(Index=74, name='Roman', team='E', Q1=73, Q2=1, Q3=25, Q4=44)\n",
      "Pandas(Index=75, name='Stanley', team='A', Q1=69, Q2=71, Q3=39, Q4=97)\n",
      "Pandas(Index=76, name='Dexter', team='E', Q1=73, Q2=94, Q3=53, Q4=20)\n",
      "Pandas(Index=77, name='Michael', team='B', Q1=89, Q2=21, Q3=59, Q4=92)\n",
      "Pandas(Index=78, name='Elliott', team='B', Q1=9, Q2=31, Q3=33, Q4=60)\n",
      "Pandas(Index=79, name='Tyler', team='A', Q1=75, Q2=16, Q3=44, Q4=63)\n",
      "Pandas(Index=80, name='Ryan', team='E', Q1=92, Q2=70, Q3=64, Q4=31)\n",
      "Pandas(Index=81, name='Ellis', team='C', Q1=34, Q2=34, Q3=77, Q4=42)\n",
      "Pandas(Index=82, name='Finn', team='E', Q1=4, Q2=1, Q3=55, Q4=32)\n",
      "Pandas(Index=83, name='Albert0', team='B', Q1=85, Q2=38, Q3=41, Q4=17)\n",
      "Pandas(Index=84, name='Kai', team='B', Q1=66, Q2=45, Q3=13, Q4=48)\n",
      "Pandas(Index=85, name='Liam', team='B', Q1=2, Q2=80, Q3=24, Q4=25)\n",
      "Pandas(Index=86, name='Calum', team='C', Q1=14, Q2=91, Q3=16, Q4=82)\n",
      "Pandas(Index=87, name='Louis2', team='C', Q1=13, Q2=94, Q3=51, Q4=22)\n",
      "Pandas(Index=88, name='Aaron', team='A', Q1=96, Q2=75, Q3=55, Q4=8)\n",
      "Pandas(Index=89, name='Ezra', team='D', Q1=16, Q2=56, Q3=86, Q4=61)\n",
      "Pandas(Index=90, name='Leon', team='E', Q1=38, Q2=60, Q3=31, Q4=7)\n",
      "Pandas(Index=91, name='Connor', team='C', Q1=62, Q2=38, Q3=63, Q4=46)\n",
      "Pandas(Index=92, name='Grayson7', team='B', Q1=59, Q2=84, Q3=74, Q4=33)\n",
      "Pandas(Index=93, name='Jamie0', team='B', Q1=39, Q2=97, Q3=84, Q4=55)\n",
      "Pandas(Index=94, name='Aiden', team='D', Q1=20, Q2=31, Q3=62, Q4=68)\n",
      "Pandas(Index=95, name='Gabriel', team='C', Q1=48, Q2=59, Q3=87, Q4=74)\n",
      "Pandas(Index=96, name='Austin7', team='C', Q1=21, Q2=31, Q3=30, Q4=43)\n",
      "Pandas(Index=97, name='Lincoln4', team='C', Q1=98, Q2=93, Q3=1, Q4=20)\n",
      "Pandas(Index=98, name='Eli', team='E', Q1=11, Q2=74, Q3=58, Q4=91)\n",
      "Pandas(Index=99, name='Ben', team='E', Q1=21, Q2=43, Q3=41, Q4=74)\n"
     ]
    }
   ],
   "source": [
    "# 按行遍历\n",
    "for row in df.itertuples():\n",
    "    print(row)\n",
    "# # 去除index索引\n",
    "# for row in df.itertuples(index = False): \n",
    "#     print(row)\n",
    "# # 指定name=TEST 进行索引\n",
    "# for row in df.itertuples(index=False, name='TEST'):\n",
    "#     print(row)\n",
    "    \n",
    "# for row in df.itertuples():\n",
    "#     print(row.Index, row.Q1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65bd92e5",
   "metadata": {},
   "source": [
    "### DF按列遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "065ab2a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:41:05.488372Z",
     "start_time": "2022-05-09T02:41:05.433721Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name Arry\n",
      "team C\n",
      "Q1 36\n",
      "Q2 37\n",
      "Q3 37\n",
      "Q4 57\n"
     ]
    }
   ],
   "source": [
    "# df.items()Iterate over (column name, Series) pairs，和df.iteritems()有相同的功能。\n",
    "\n",
    "\n",
    "for label, ser in df.loc[1].items():\n",
    "    print(label,ser)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "83ffd743",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:44:22.437187Z",
     "start_time": "2022-05-09T02:44:22.280331Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "0        Liver\n",
      "1         Arry\n",
      "2          Ack\n",
      "3        Eorge\n",
      "4          Oah\n",
      "        ...   \n",
      "95     Gabriel\n",
      "96     Austin7\n",
      "97    Lincoln4\n",
      "98         Eli\n",
      "99         Ben\n",
      "Name: name, Length: 100, dtype: object\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n"
     ]
    }
   ],
   "source": [
    "# 依次取出每一列\n",
    "for name in df: \n",
    "    print(df['name'])\n",
    "    \n",
    "for i in df['name']: \n",
    "    print(i)\n",
    "    \n",
    "# 对每个列的内容进行迭代\n",
    "for name in df: \n",
    "    for i in df['name']:\n",
    "        print(i)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17552255",
   "metadata": {},
   "source": [
    "### Series的迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "a5705082",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:49:58.725714Z",
     "start_time": "2022-05-09T02:49:58.691667Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Liver\n",
      "Arry\n",
      "Ack\n",
      "Eorge\n",
      "Oah\n",
      "Harlie\n",
      "Acob\n",
      "Lfie\n",
      "Reddie\n",
      "Oscar\n",
      "Leo\n",
      "Logan\n",
      "Archie\n",
      "Theo\n",
      "Thomas\n",
      "James\n",
      "Joshua\n",
      "Henry\n",
      "William\n",
      "Max\n",
      "Lucas\n",
      "Ethan\n",
      "Arthur\n",
      "Mason\n",
      "Isaac\n",
      "Harrison\n",
      "Teddy\n",
      "Finley\n",
      "Daniel\n",
      "Riley\n",
      "Edward\n",
      "Joseph\n",
      "Alexander\n",
      "Adam\n",
      "Reggie1\n",
      "Samuel\n",
      "Jaxon\n",
      "Sebastian\n",
      "Elijah\n",
      "Harley\n",
      "Toby\n",
      "Arlo8\n",
      "Dylan\n",
      "Jude\n",
      "Benjamin\n",
      "Rory9\n",
      "Tommy\n",
      "Jake3\n",
      "Louie\n",
      "Carter7\n",
      "Jenson\n",
      "Hugo0\n",
      "Bobby1\n",
      "Frankie\n",
      "Ollie3\n",
      "Zachary\n",
      "David\n",
      "Albie1\n",
      "Lewis\n",
      "Luca\n",
      "Ronnie\n",
      "Jackson5\n",
      "Matthew\n",
      "Alex\n",
      "Harvey2\n",
      "Reuben\n",
      "Jayden6\n",
      "Caleb\n",
      "Hunter3\n",
      "Theodore3\n",
      "Nathan\n",
      "Blake\n",
      "Luke6\n",
      "Elliot\n",
      "Roman\n",
      "Stanley\n",
      "Dexter\n",
      "Michael\n",
      "Elliott\n",
      "Tyler\n",
      "Ryan\n",
      "Ellis\n",
      "Finn\n",
      "Albert0\n",
      "Kai\n",
      "Liam\n",
      "Calum\n",
      "Louis2\n",
      "Aaron\n",
      "Ezra\n",
      "Leon\n",
      "Connor\n",
      "Grayson7\n",
      "Jamie0\n",
      "Aiden\n",
      "Gabriel\n",
      "Austin7\n",
      "Lincoln4\n",
      "Eli\n",
      "Ben\n",
      "(0, 'Liver')\n",
      "(1, 'Arry')\n",
      "(2, 'Ack')\n",
      "(3, 'Eorge')\n",
      "(4, 'Oah')\n",
      "(5, 'Harlie')\n",
      "(6, 'Acob')\n",
      "(7, 'Lfie')\n",
      "(8, 'Reddie')\n",
      "(9, 'Oscar')\n",
      "(10, 'Leo')\n",
      "(11, 'Logan')\n",
      "(12, 'Archie')\n",
      "(13, 'Theo')\n",
      "(14, 'Thomas')\n",
      "(15, 'James')\n",
      "(16, 'Joshua')\n",
      "(17, 'Henry')\n",
      "(18, 'William')\n",
      "(19, 'Max')\n",
      "(20, 'Lucas')\n",
      "(21, 'Ethan')\n",
      "(22, 'Arthur')\n",
      "(23, 'Mason')\n",
      "(24, 'Isaac')\n",
      "(25, 'Harrison')\n",
      "(26, 'Teddy')\n",
      "(27, 'Finley')\n",
      "(28, 'Daniel')\n",
      "(29, 'Riley')\n",
      "(30, 'Edward')\n",
      "(31, 'Joseph')\n",
      "(32, 'Alexander')\n",
      "(33, 'Adam')\n",
      "(34, 'Reggie1')\n",
      "(35, 'Samuel')\n",
      "(36, 'Jaxon')\n",
      "(37, 'Sebastian')\n",
      "(38, 'Elijah')\n",
      "(39, 'Harley')\n",
      "(40, 'Toby')\n",
      "(41, 'Arlo8')\n",
      "(42, 'Dylan')\n",
      "(43, 'Jude')\n",
      "(44, 'Benjamin')\n",
      "(45, 'Rory9')\n",
      "(46, 'Tommy')\n",
      "(47, 'Jake3')\n",
      "(48, 'Louie')\n",
      "(49, 'Carter7')\n",
      "(50, 'Jenson')\n",
      "(51, 'Hugo0')\n",
      "(52, 'Bobby1')\n",
      "(53, 'Frankie')\n",
      "(54, 'Ollie3')\n",
      "(55, 'Zachary')\n",
      "(56, 'David')\n",
      "(57, 'Albie1')\n",
      "(58, 'Lewis')\n",
      "(59, 'Luca')\n",
      "(60, 'Ronnie')\n",
      "(61, 'Jackson5')\n",
      "(62, 'Matthew')\n",
      "(63, 'Alex')\n",
      "(64, 'Harvey2')\n",
      "(65, 'Reuben')\n",
      "(66, 'Jayden6')\n",
      "(67, 'Caleb')\n",
      "(68, 'Hunter3')\n",
      "(69, 'Theodore3')\n",
      "(70, 'Nathan')\n",
      "(71, 'Blake')\n",
      "(72, 'Luke6')\n",
      "(73, 'Elliot')\n",
      "(74, 'Roman')\n",
      "(75, 'Stanley')\n",
      "(76, 'Dexter')\n",
      "(77, 'Michael')\n",
      "(78, 'Elliott')\n",
      "(79, 'Tyler')\n",
      "(80, 'Ryan')\n",
      "(81, 'Ellis')\n",
      "(82, 'Finn')\n",
      "(83, 'Albert0')\n",
      "(84, 'Kai')\n",
      "(85, 'Liam')\n",
      "(86, 'Calum')\n",
      "(87, 'Louis2')\n",
      "(88, 'Aaron')\n",
      "(89, 'Ezra')\n",
      "(90, 'Leon')\n",
      "(91, 'Connor')\n",
      "(92, 'Grayson7')\n",
      "(93, 'Jamie0')\n",
      "(94, 'Aiden')\n",
      "(95, 'Gabriel')\n",
      "(96, 'Austin7')\n",
      "(97, 'Lincoln4')\n",
      "(98, 'Eli')\n",
      "(99, 'Ben')\n"
     ]
    }
   ],
   "source": [
    "# 定义Series\n",
    "s = df['name']\n",
    "\n",
    "# 遍历Series\n",
    "for i in s:\n",
    "    print(i)\n",
    "    \n",
    "# 按(索引，值)进行索引\n",
    "for i in s.items(): \n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8025d20a",
   "metadata": {},
   "source": [
    "## Pandas的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "33afdc26",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T07:05:47.896127Z",
     "start_time": "2022-05-09T07:05:47.813717Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['name', 'team', 'Q1', 'Q2', 'Q3', 'Q4'], dtype='object')"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建数据\n",
    "data = 'team.xlsx'\n",
    "df = pd.read_excel(data, sheet_name= 'Sheet1')\n",
    "\n",
    "df.columns  # 查看列头"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7d2169d",
   "metadata": {},
   "source": [
    "### pipe()管道方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "7c6b1b28",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T02:54:37.591425Z",
     "start_time": "2022-05-09T02:54:37.570678Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'f' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [143]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 分析过程标准化、流水线化，达到复用。\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 语法结构\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# df.pipe(<函数名>, <传给函数的参数表>)\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 对 df 多重应用多个函数, f函数-g函数-h函数\u001b[39;00m\n\u001b[1;32m----> 6\u001b[0m \u001b[43mf\u001b[49m(g(h(df), arg1\u001b[38;5;241m=\u001b[39ma), arg2\u001b[38;5;241m=\u001b[39mb, arg3\u001b[38;5;241m=\u001b[39mc)\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 用pipe()可以将他们连接起来\u001b[39;00m\n\u001b[0;32m      9\u001b[0m (df\u001b[38;5;241m.\u001b[39mpipe(h)\n\u001b[0;32m     10\u001b[0m    \u001b[38;5;241m.\u001b[39mpipe(g, arg1\u001b[38;5;241m=\u001b[39ma)\n\u001b[0;32m     11\u001b[0m    \u001b[38;5;241m.\u001b[39mpipe(f, arg2\u001b[38;5;241m=\u001b[39mb, arg3\u001b[38;5;241m=\u001b[39mc)\n\u001b[0;32m     12\u001b[0m )\n",
      "\u001b[1;31mNameError\u001b[0m: name 'f' is not defined"
     ]
    }
   ],
   "source": [
    "# 分析过程标准化、流水线化，达到复用。\n",
    "# 对数据连续操作形成方法链（多个方法连续调用对数据进行处理）\n",
    "# 语法结构\n",
    "# df.pipe(<函数名>, <传给函数的参数表>)\n",
    "\n",
    "# 对 df 多重应用多个函数, f函数-g函数-h函数\n",
    "f(g(h(df), arg1=a), arg2=b, arg3=c)\n",
    "\n",
    "# 用pipe()可以将他们连接起来\n",
    "(df.pipe(h)\n",
    "   .pipe(g, arg1=a)\n",
    "   .pipe(f, arg2=b, arg3=c)\n",
    ")\n",
    "# 以下是将 'arg2' 参数给函数 f 然后作为函数整体授受后边的参数\n",
    "(df.pipe(h)\n",
    "   .pipe(g, arg1=a)\n",
    "   .pipe((f, 'arg2'), arg1=a, arg3=c)\n",
    " )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "e2dd725f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Liver</td>\n",
       "      <td>E</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arry</td>\n",
       "      <td>C</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ack</td>\n",
       "      <td>A</td>\n",
       "      <td>57</td>\n",
       "      <td>60</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Eorge</td>\n",
       "      <td>C</td>\n",
       "      <td>93</td>\n",
       "      <td>96</td>\n",
       "      <td>71</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Oah</td>\n",
       "      <td>D</td>\n",
       "      <td>65</td>\n",
       "      <td>49</td>\n",
       "      <td>61</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>Gabriel</td>\n",
       "      <td>C</td>\n",
       "      <td>48</td>\n",
       "      <td>59</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7</td>\n",
       "      <td>C</td>\n",
       "      <td>21</td>\n",
       "      <td>31</td>\n",
       "      <td>30</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4</td>\n",
       "      <td>C</td>\n",
       "      <td>98</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Eli</td>\n",
       "      <td>E</td>\n",
       "      <td>11</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>Ben</td>\n",
       "      <td>E</td>\n",
       "      <td>21</td>\n",
       "      <td>43</td>\n",
       "      <td>41</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        name team  Q1  Q2  Q3  Q4\n",
       "0      Liver    E  89  21  24  64\n",
       "1       Arry    C  36  37  37  57\n",
       "2        Ack    A  57  60  18  84\n",
       "3      Eorge    C  93  96  71  78\n",
       "4        Oah    D  65  49  61  86\n",
       "..       ...  ...  ..  ..  ..  ..\n",
       "95   Gabriel    C  48  59  87  74\n",
       "96   Austin7    C  21  31  30  43\n",
       "97  Lincoln4    C  98  93   1  20\n",
       "98       Eli    E  11  74  58  91\n",
       "99       Ben    E  21  43  41  74\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "605dea03",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:26:51.089355Z",
     "start_time": "2022-05-09T06:26:51.023265Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "      <th>avg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>94</td>\n",
       "      <td>26</td>\n",
       "      <td>29</td>\n",
       "      <td>69</td>\n",
       "      <td>54.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>41</td>\n",
       "      <td>42</td>\n",
       "      <td>42</td>\n",
       "      <td>62</td>\n",
       "      <td>46.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>62</td>\n",
       "      <td>65</td>\n",
       "      <td>23</td>\n",
       "      <td>89</td>\n",
       "      <td>59.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>98</td>\n",
       "      <td>101</td>\n",
       "      <td>76</td>\n",
       "      <td>83</td>\n",
       "      <td>89.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70</td>\n",
       "      <td>54</td>\n",
       "      <td>66</td>\n",
       "      <td>91</td>\n",
       "      <td>70.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>53</td>\n",
       "      <td>64</td>\n",
       "      <td>92</td>\n",
       "      <td>79</td>\n",
       "      <td>72.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>26</td>\n",
       "      <td>36</td>\n",
       "      <td>35</td>\n",
       "      <td>48</td>\n",
       "      <td>36.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>103</td>\n",
       "      <td>98</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "      <td>58.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>16</td>\n",
       "      <td>79</td>\n",
       "      <td>63</td>\n",
       "      <td>96</td>\n",
       "      <td>63.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>26</td>\n",
       "      <td>48</td>\n",
       "      <td>46</td>\n",
       "      <td>79</td>\n",
       "      <td>49.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Q1   Q2  Q3  Q4    avg\n",
       "0    94   26  29  69  54.50\n",
       "1    41   42  42  62  46.75\n",
       "2    62   65  23  89  59.75\n",
       "3    98  101  76  83  89.50\n",
       "4    70   54  66  91  70.25\n",
       "..  ...  ...  ..  ..    ...\n",
       "95   53   64  92  79  72.00\n",
       "96   26   36  35  48  36.25\n",
       "97  103   98   6  25  58.00\n",
       "98   16   79  63  96  63.50\n",
       "99   26   48  46  79  49.75\n",
       "\n",
       "[100 rows x 5 columns]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 示例\n",
    "# 定义一个函数，给所有Q的成绩加n，然后增加平均数， 其中n为要加的值\n",
    "\n",
    "def add_mean(rdf, n): \n",
    "    df = rdf.copy()\n",
    "    df = df.loc[:, 'Q1':'Q4'].applymap(lambda x: x+n)  # Q1-Q4每个数值都加n\n",
    "    df['avg'] = df.loc[:,'Q1':'Q4'].mean(1)    # 添加一列 avg： 取Q1:Q4的均值\n",
    "    return df\n",
    "# 调用\n",
    "df.pipe(add_mean, 5)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f5eb893",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:28:35.561640Z",
     "start_time": "2022-05-09T06:28:35.542641Z"
    }
   },
   "source": [
    "### apply() 对DataFrame中按行和列进行函数处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "60880254",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:39:25.575609Z",
     "start_time": "2022-05-09T06:39:25.513728Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>team</th>\n",
       "      <th>Q1</th>\n",
       "      <th>Q2</th>\n",
       "      <th>Q3</th>\n",
       "      <th>Q4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>LiverLiver</td>\n",
       "      <td>EE</td>\n",
       "      <td>178</td>\n",
       "      <td>42</td>\n",
       "      <td>48</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ArryArry</td>\n",
       "      <td>CC</td>\n",
       "      <td>72</td>\n",
       "      <td>74</td>\n",
       "      <td>74</td>\n",
       "      <td>114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AckAck</td>\n",
       "      <td>AA</td>\n",
       "      <td>114</td>\n",
       "      <td>120</td>\n",
       "      <td>36</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EorgeEorge</td>\n",
       "      <td>CC</td>\n",
       "      <td>186</td>\n",
       "      <td>192</td>\n",
       "      <td>142</td>\n",
       "      <td>156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>OahOah</td>\n",
       "      <td>DD</td>\n",
       "      <td>130</td>\n",
       "      <td>98</td>\n",
       "      <td>122</td>\n",
       "      <td>172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>GabrielGabriel</td>\n",
       "      <td>CC</td>\n",
       "      <td>96</td>\n",
       "      <td>118</td>\n",
       "      <td>174</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>Austin7Austin7</td>\n",
       "      <td>CC</td>\n",
       "      <td>42</td>\n",
       "      <td>62</td>\n",
       "      <td>60</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>Lincoln4Lincoln4</td>\n",
       "      <td>CC</td>\n",
       "      <td>196</td>\n",
       "      <td>186</td>\n",
       "      <td>2</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>EliEli</td>\n",
       "      <td>EE</td>\n",
       "      <td>22</td>\n",
       "      <td>148</td>\n",
       "      <td>116</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>BenBen</td>\n",
       "      <td>EE</td>\n",
       "      <td>42</td>\n",
       "      <td>86</td>\n",
       "      <td>82</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                name team   Q1   Q2   Q3   Q4\n",
       "0         LiverLiver   EE  178   42   48  128\n",
       "1           ArryArry   CC   72   74   74  114\n",
       "2             AckAck   AA  114  120   36  168\n",
       "3         EorgeEorge   CC  186  192  142  156\n",
       "4             OahOah   DD  130   98  122  172\n",
       "..               ...  ...  ...  ...  ...  ...\n",
       "95    GabrielGabriel   CC   96  118  174  148\n",
       "96    Austin7Austin7   CC   42   62   60   86\n",
       "97  Lincoln4Lincoln4   CC  196  186    2   40\n",
       "98            EliEli   EE   22  148  116  182\n",
       "99            BenBen   EE   42   86   82  148\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(max)  # 最大值\n",
    "df.apply(lambda x: x*2)  # 每一列乘以2\n",
    "# df.Q1.apply(lambda x: x+10 if type(x) is int else x)  # 取Q1列， 当数值类型为int， 返回x+10， 否则，返回x\n",
    "\n",
    "# # 将df数据，每个人成绩都加10分\n",
    "# df.info() # 查看基本信息，发现Q1-Q4数据类型为int\n",
    "# df.apply(lambda x: x+10 if type(x) is int else x)  # 将结果中int类型数据全加1\n",
    "\n",
    "\n",
    "# df.loc[:,'Q1':'Q4'].apply(sum)  # 每列求和\n",
    "# df.loc[:,'Q1':'Q4'].apply(sum, axis = 1)  # 每行求和\n",
    "\n",
    "\n",
    "# # 执行定义的函数\n",
    "# df mymax(x): \n",
    "#     return x.max()\n",
    "# df.apply(lambda x : mymax(x))\n",
    "\n",
    "# # 判断一个值是否在另一个类似列表的列中\n",
    "# df.apply(lambda x : x.s in x.s_list, axis = 1) # 布尔序列\n",
    "# df.apply(lambda x : x.s in x.s_list, axis = 1).astype(int) # 将布尔序列转换成0和1序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "124d4f57",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:52:38.040333Z",
     "start_time": "2022-05-09T06:52:38.005120Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     other\n",
       "1     other\n",
       "2     other\n",
       "3     other\n",
       "4     other\n",
       "      ...  \n",
       "95    other\n",
       "96    other\n",
       "97    other\n",
       "98    other\n",
       "99    other\n",
       "Length: 100, dtype: object"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一个常用的根据条件输出结果的案例\n",
    "\n",
    "func = lambda x : np.where(x.team == 'A' and x.Q1>80, 'Good','Other')\n",
    "df.apply(func, axis=1)\n",
    "\n",
    "df.apply(lambda x:x.team=='A' and x.Q1>90, axis=1).map({True:'good', False:'other'})\n",
    "\n",
    "df.apply(lambda x: 'good' if x.team=='A' and x.Q1>90 else 'other', axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "4d9ead99",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T06:54:07.382887Z",
     "start_time": "2022-05-09T06:54:07.345252Z"
    }
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Series' object has no attribute 'team'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[1;32mIn [147]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 总结，apply 可以应用的函数类型包括：\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# 自定义\u001b[39;00m\n\u001b[0;32m      4\u001b[0m df\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28mmax\u001b[39m) \u001b[38;5;66;03m# python 内置函数\u001b[39;00m\n\u001b[0;32m      5\u001b[0m df\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x: x\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m) \u001b[38;5;66;03m# lambda\u001b[39;00m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py:8839\u001b[0m, in \u001b[0;36mDataFrame.apply\u001b[1;34m(self, func, axis, raw, result_type, args, **kwargs)\u001b[0m\n\u001b[0;32m   8828\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mapply\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m frame_apply\n\u001b[0;32m   8830\u001b[0m op \u001b[38;5;241m=\u001b[39m frame_apply(\n\u001b[0;32m   8831\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m   8832\u001b[0m     func\u001b[38;5;241m=\u001b[39mfunc,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   8837\u001b[0m     kwargs\u001b[38;5;241m=\u001b[39mkwargs,\n\u001b[0;32m   8838\u001b[0m )\n\u001b[1;32m-> 8839\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39m__finalize__(\u001b[38;5;28mself\u001b[39m, method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapply\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py:727\u001b[0m, in \u001b[0;36mFrameApply.apply\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    724\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mraw:\n\u001b[0;32m    725\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mapply_raw()\n\u001b[1;32m--> 727\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply_standard\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py:851\u001b[0m, in \u001b[0;36mFrameApply.apply_standard\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    850\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mapply_standard\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m--> 851\u001b[0m     results, res_index \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply_series_generator\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    853\u001b[0m     \u001b[38;5;66;03m# wrap results\u001b[39;00m\n\u001b[0;32m    854\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwrap_results(results, res_index)\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\apply.py:867\u001b[0m, in \u001b[0;36mFrameApply.apply_series_generator\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    864\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m option_context(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmode.chained_assignment\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m    865\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m i, v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(series_gen):\n\u001b[0;32m    866\u001b[0m         \u001b[38;5;66;03m# ignore SettingWithCopy here in case the user mutates\u001b[39;00m\n\u001b[1;32m--> 867\u001b[0m         results[i] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mv\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    868\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(results[i], ABCSeries):\n\u001b[0;32m    869\u001b[0m             \u001b[38;5;66;03m# If we have a view on v, we need to make a copy because\u001b[39;00m\n\u001b[0;32m    870\u001b[0m             \u001b[38;5;66;03m#  series_generator will swap out the underlying data\u001b[39;00m\n\u001b[0;32m    871\u001b[0m             results[i] \u001b[38;5;241m=\u001b[39m results[i]\u001b[38;5;241m.\u001b[39mcopy(deep\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n",
      "Input \u001b[1;32mIn [146]\u001b[0m, in \u001b[0;36m<lambda>\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 一个常用的根据条件输出结果的案例\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x : np\u001b[38;5;241m.\u001b[39mwhere(\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mteam\u001b[49m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m x\u001b[38;5;241m.\u001b[39mQ1\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m80\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mGood\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mOther\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m      4\u001b[0m df\u001b[38;5;241m.\u001b[39mapply(func, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m      6\u001b[0m df\u001b[38;5;241m.\u001b[39mapply(\u001b[38;5;28;01mlambda\u001b[39;00m x:x\u001b[38;5;241m.\u001b[39mteam\u001b[38;5;241m==\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m x\u001b[38;5;241m.\u001b[39mQ1\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m90\u001b[39m, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m.\u001b[39mmap({\u001b[38;5;28;01mTrue\u001b[39;00m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgood\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mother\u001b[39m\u001b[38;5;124m'\u001b[39m})\n",
      "File \u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py:5575\u001b[0m, in \u001b[0;36mNDFrame.__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   5568\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[0;32m   5569\u001b[0m     name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_internal_names_set\n\u001b[0;32m   5570\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_metadata\n\u001b[0;32m   5571\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_accessors\n\u001b[0;32m   5572\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_info_axis\u001b[38;5;241m.\u001b[39m_can_hold_identifiers_and_holds_name(name)\n\u001b[0;32m   5573\u001b[0m ):\n\u001b[0;32m   5574\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m[name]\n\u001b[1;32m-> 5575\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mobject\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__getattribute__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Series' object has no attribute 'team'"
     ]
    }
   ],
   "source": [
    "# 总结，apply 可以应用的函数类型包括：\n",
    "\n",
    "df.apply(func) # 自定义\n",
    "df.apply(max) # python 内置函数\n",
    "df.apply(lambda x: x*2) # lambda\n",
    "df.apply(np.mean) # numpy 等其他库的函数\n",
    "df.apply(pd.Series.first_valid_index) # Pandas 自己的函数\n",
    "df.apply('count') # Pandas 自己的函数方法\n",
    "# 多个函数\n",
    "df.apply([sum, 'count']) # 相当于 .aggregate, 即.agg\n",
    "df.apply({'Q1':sum, 'Q2':'count'}) # 同上\n",
    "\n",
    "# 特别要说明的如果函数参数传的是字符串，先会尝试当前对象的同名方法（如 DataFrame 就是 DataFrame 的，Series 就是 Series 的），\n",
    "# 如没有会尝试 NumPy 有没有这个同名 ufunc （NumPy 中的通用函数），以上有会应用，如均没有就会报错。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3abfadc",
   "metadata": {},
   "source": [
    "### applymap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14a21a29",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以做到元素级的函数应用，就是对df中所有元素（不包含索引）应用函数\n",
    "\n",
    "df.applymap(lambda x : x*2)\n",
    "df.applymap(lambda x : x+10 if type(x) is int else x)\n",
    "df.applymap(lambda x : len(str(x)))\n",
    "\n",
    "# 自定义\n",
    "df mylen(x):\n",
    "    return len(str(x))\n",
    "df.applymap(mylen)\n",
    "\n",
    "# 对空值不使用函数\n",
    "df.applymap(mylen, na_action= 'ignore')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae54f227",
   "metadata": {},
   "source": [
    "### map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2e072930",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T07:08:18.823246Z",
     "start_time": "2022-05-09T07:08:18.773218Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     I am a E\n",
       "1     I am a C\n",
       "2     I am a A\n",
       "3     I am a C\n",
       "4     I am a D\n",
       "        ...   \n",
       "95    I am a C\n",
       "96    I am a C\n",
       "97    I am a C\n",
       "98    I am a E\n",
       "99    I am a E\n",
       "Name: team, Length: 100, dtype: object"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据输入对应关系映射值修改内容，用于Series或者DataFrame对象的一列\n",
    "\n",
    "# dataframe\n",
    "df.team.map({'A':'一班','B':'二班','C':'三班','D':'四班',})  # 枚举替换\n",
    "df.team.map('I am a {}'.format)\n",
    "df.team.map('I am a {}'.format, na_action= 'ignore')\n",
    "\n",
    "# # series\n",
    "# t = pd.Series({'six': 6., 'seven': 7.})\n",
    "# s.map(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44d25bf1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T07:13:57.229781Z",
     "start_time": "2022-05-09T07:13:57.194152Z"
    }
   },
   "outputs": [],
   "source": [
    "# 应用函数\n",
    "# 定义函数\n",
    "def f(x): \n",
    "    return len(str(x))\n",
    "# 调用函数\n",
    "df['name'].map(f)\n",
    "\n",
    "\n",
    "# 三种情况的判断\n",
    "func= lambda x: (x>60 and '及格') or (x == 60 and '60分') or (x<60 and '不及格')\n",
    "df.Q1.map(func)\n",
    "\n",
    "# 利用np.sign 判断值为正、负 、0 的情况，并映射\n",
    "label = {0:'平', 1:'涨', -1:'跌' }\n",
    "ser.map(np.sign).map(label)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7f185d3",
   "metadata": {},
   "source": [
    "### agg(）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb18f284",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T07:49:04.640459Z",
     "start_time": "2022-05-09T07:49:04.612830Z"
    }
   },
   "outputs": [],
   "source": [
    "# 使用指定轴上的一项或者多项操作进行汇总\n",
    "\n",
    "\n",
    "df.agg('max')  # 每列的最大值\n",
    "df.agg(['sum','min'])  # 每列的sum 和min\n",
    "df.agg({'Q1':['sum','min'],'Q2':['sum','min']})  # 指定列 Q1,Q2\n",
    "\n",
    "\n",
    "# 分组后按列聚合\n",
    "df.groupby('team').agg('max')  # 类似于SQL的group by\n",
    "\n",
    "\n",
    "# 定义函数\n",
    "df.Q1.agg(['sum', 'mean'])\n",
    "\n",
    "def mymean(x): \n",
    "    return x.mean()\n",
    "df.Q1.agg(['sum', mymean])\n",
    "\n",
    "# 每一列使用不同的方法进行汇聚\n",
    "df.agg(a = ('Q1', max)\n",
    "      b = ('Q2', 'min')\n",
    "      c = ('Q3', np.mean)\n",
    "        d= ('Q4', lambda s: s.sum()+1)\n",
    "      )\n",
    "\n",
    "\n",
    "# 按行聚类\n",
    "df.loc[:,'Q1':].agg('mean', axis = 'columns')\n",
    "\n",
    "# 利用 pd.Series.add 方法对所有数据加分\n",
    "# other 是 add 方法的参数\n",
    "df.loc[:,'Q1':].agg(pd.Series.add, other=10)\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5fcc5e9",
   "metadata": {},
   "source": [
    "### transform()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac96108e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T07:55:09.787459Z",
     "start_time": "2022-05-09T07:55:09.694365Z"
    }
   },
   "outputs": [],
   "source": [
    "# 自身调用函数并返回一个dataframe\n",
    "\n",
    "df.transform(lambda x : x*2) # 单个函数\n",
    "df.transform([np.sqrt, np.exp]) # 多个函数\n",
    "\n",
    "df.transform(np.abs, lambda x: x+1)\n",
    "df.transform('abs')\n",
    "df.transform(lambda x: x.abs())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "435b9c0a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T08:06:01.564162Z",
     "start_time": "2022-05-09T08:06:01.526214Z"
    }
   },
   "outputs": [],
   "source": [
    "# 可以对比下下两个操作：\n",
    "\n",
    "# 聚合后按组显示合计\n",
    "df.groupby('team').sum()\n",
    "# 聚合后按原数据结构显示数据，但在指定位置上显示聚合计算后的结果\n",
    "df.groupby('team').transform(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "660f7d0b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T08:06:04.962044Z",
     "start_time": "2022-05-09T08:06:04.883798Z"
    }
   },
   "source": [
    "### copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "12180fa1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T08:22:32.647984Z",
     "start_time": "2022-05-09T08:22:32.639026Z"
    }
   },
   "outputs": [],
   "source": [
    "# 类似于Python中的copy()函数，可以返回一个新的对象，这个新的对象就与原对象断绝关系了\n",
    "s = pd.Series([1,2], index=['a','b'])\n",
    "s_1 = s\n",
    "\n",
    "s_copy = s.copy()\n",
    "\n",
    "# 验证 = 和 copy()的区别\n",
    "s_1 is s  # True\n",
    "s_copy = s # False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5470d2a4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-09T08:22:36.789628Z",
     "start_time": "2022-05-09T08:22:36.775813Z"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45065d4b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "671.818px",
    "left": "59px",
    "top": "111.051px",
    "width": "292.75px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
